I know absolutely nothing about macros. Anyway I think I need one to be able to order data in a document.
I have three files/documents with data in them ordered in the usual chronological way from the oldest to the most recent one.
Anyway I want to merge this 3 files into a single document. So I think I need a macro that -once I have copied all the three documents in a new single one- will order those three separates text parts in an unique one with beautiful chronological order from the oldest date data to the most recent date data...
The date in the text is written in this format: D or DD-M or MM-YYYY like in 10-8-2004
I afraid that creating such macro will take more time than ordering manually, unless the documents are really large.
I think, it is possible to create such macro, but only if the documents contain only simple text (without any formatting, tables and other objects). We would join all, then parse (using regular expressions) into array, order the array, and join again. hock:
i think about reading all three files as string variables and then
to merge the three strings.
make an array of it and convert your date entries in each line
into a format what qm can sort.
I have been quiet busy and Sunday I could not get to the website.
Now what I'm I supposed to do? I downloaded the code into my PC but Windows can't read it.
I'm ready to buy Quick Macros 2 but how I use it in relation with Words Documents to get what I want? I need step by step explanation even for taken for granted actions...
1) I have a QM VARIANT array that is 3 columns and 50,000 rows
2) The first column is just a record number (ie, from 1 to 50,000)
3) I need to sort the entire array based on the second column, which is strings of width 0 to 100 characters.
4) Then I will perform an operation to populate the third column. This operation would be based on the strings in the second column.
5) Then I would like to resort the entire array based on the first column (ie, just put it back in record number order).
So....my overarching question is...does this sound doable (I would prefer not to export to Excel and then bring back into QM)...and if so, could you kindly kick me off with a little code?
I can't tell from the code in this post if I must glue all the columns together and sort on fixed positions, or if there is an easier way to work with qsort and arrays...or maybe that is the easy way
If possible, I just need to understand the essence of sorting a multi-column array based on one column. I don't see how to designate one column for qsort to operate on.
;Create test array ARRAY(VARIANT) a.create(3 12) int i j; str s ss
ss= ;january
;february
;march
;april
;may
;june
;july
;august
;september
;october
;november
;december for i 0 a.len ,a[0 i]=i+1 ,s.getl(ss -i) ,a[1 i]=s out "[]Unsorted array:" for(i 0 a.len) out a[1 i]
;Sort by second column SortVariantArray2Dim a 1 out "[]Sorted by second column:" for(i 0 a.len) out a[1 i]
;Sort by first column SortVariantArray2Dim a 0 out "[]Sorted by first column:" for(i 0 a.len) out a[1 i]
These are working great, but I have come up on one issue. My data often has metadata in the first row of the array. I need to exclude this from the sorts. I have a workaround for removing the first row, using the new sort function, then reassembling an array with the metadata in the first row. But I'm not sure how much that will slow down my QM process with large arrays.
Ideally if there is a way to add (without too much trouble on your part) a flag to SortVariantArray2Dim for excluding the first row, that would make this function perfect. At least for now
Although if there is a way to do an insert into an array (ie, just insert the metadata back into the first row when the sorts are done), I think that would speed my QM process and reduce the need for a new flag. But I don't see a way to do inserts.
Thanks Ginid. The functions are working great. One last question. I'm now trying to push one column from the sorted VARIANT array (b) out to a defined range in Excel (f). I'm using the process below, but it takes some time. Might there be a faster way?
;Create test array ARRAY(VARIANT) b.create(3 12) int i j; str s ss
ss= ;january
;february
;march
;april
;may
;june
;july
;august
;september
;october
;november
;december for i 0 b.len ,b[0 i]=i+1 ,s.getl(ss -i) ,b[2 i]=s
Thanks again Gindi. Everything is working great, especially with smaller arrays. But QM isn't as fast as Excel with bigger arrays. I realize this is likely by design....to keep the QM footprint small. Just curious, is there a way to make my PC give more memory to QM...and is there a way to let QM use it? Or perhaps this is a long and complicated matter....
I didn't test the speed of f.Value=aa. If it slower than in Excel scripts, probably the Value function is implemented so that for each array element it switches between QM and Excel processes. That is, the reason of slowlyness is frequent context switches. For example, for this reason acc and htm functions are so slow.
QM, like any other process, can get as much memory as it need. Computer (all programs, not only QM) becomes slow if amount of memory is too small. For example, Windows XP SP2 will run faster with 512 MB or more memory than with 256 MB.
Thanks for that info. Actually yes, f.Value=aa is very fast. That was a bad post by by me (had been away for a few days).
What I was really asking is if the array sorting functions you created shoud be expected to be slower than Excel. When I sort an array with columns of 50,000 values in Excel, it happens very quickly. But my QM macro doing the same thing takes a bit of time (but it is easier because some of the other operations happen automatically!).
I was curious to know if I should expect QM to be slower. If it should sort at the same speed as Excel then I will look at my code harder to see if something else is slowing it down.
Yes, QM should sort slower. QM code is not compiled to native machine code. Also, calling an user-defined function is quite slow.
I tested sorting array of 46425 elements (all single-word, almost all unique). It took 0.725 s in QM. Function __SortVarArrAsc was called 811736 times. Excel sorted faster, although I cannot measure.