Extensions and Plugins > Xdata by Em Software

Using Arrays to read ahead


This doesn't even get a mention in the manual I have! (may be in later ones?)

Sometimes you need to look ahead more than just one record - 'next' is fine for the next record, but it won't take you further.

My data has a 'colour' field, and if an item is available in more than one colour, they go into the heading, with the individual prices listed below. So the data:

Small Vanity Unit   30901420   With drawer 48.5 x 36h x 40cm   £645.00   WHITE      
Small Vanity Unit   30901427   With drawer 48.5 x 36h x 40cm   £645.00   LIGHT GREEN      
Small Vanity Unit   30901429   With drawer 48.5 x 36h x 40cm   £645.00   DARK BROWN      

Needs to look like this on the Quark page:

Small Vanity Unit
REF       DESCRIPTION                           WHITE     LIGHT GREEN   DARK BROWN
30901   With drawer 48.5 x 36h x 40cm   £645.00   £645.00          £645.00

- that is, you need not only to get the text 'Light Green' from the next record, but also 'Dark Brown' from the one after that, but then go back to the first for the description and price (the price could vary).

The solution (thanks to Emsoftware for telling me about it!) is to read the data into an array until I get to a new header:

«set itemdelimiter to "|"
«repeat 10000
«put bigArray & ProductCode & "|" & ProductDescription & "|" & FinishType & "|" & Price & "|" & return into bigArray
«if ((next ProductHeader <> ProductHeader)
«exit repeat
«end repeat

- this creates an array, called bigArray, which contains all the lines that share a ProductHeader. The items on each line are separated by a pipe character.

Then XData is ready to start placing items on the page, but instead of using its fieldname, e.g. «ProductCode», you would tell is «item 1 of line ?? of bigArray», like this:

«put the number of lines in bigArray into numlines
«item 1 of line 1 of bigArray» & tab & «item 2 of line 1 of bigArray»
«repeat with i from 1 to numlines
«item 3 of line i of bigArray» & tab
«end repeat

This is a rather cobbled together explanation from a much longer prototype, with lots of added complications, but I hope it gives a flavour of the power of arrays. I expect to learn lots more ways of using them, now I'm aware of their existence!

My final prototype is well over a page long, and probably contains 90% logic, 10% actual elements going on the page! I've come a long way from «field1» «field2» «field3» !!

Anyone else played with these? I'm sure afig has...

And now I have another use for arrays!

Customer has supplied price list in Excel. Each product is available in a range of sizes, and each product goes into a separate table. But they have listed them from largest to smallest, and on the printed price list they want it to go from smallest to largest.

Just read into an array, then place the data 'backwards':

«repeat with ctr = (the number of lines in bigArray) down to 1»
«item 1 of line ctr of bigArray»..... etc
«end repeat»

Works a treat!


[0] Message Index

Go to full version