AW: AW: Lines, items, repeat loops, > not always what you think

Thomas Fischer fischer at mail.sub.uni-goettingen.de
Thu Oct 20 10:05:34 EDT 2005


Hi Jim,

I'm sitting in a train and just found some time to go through your arguments.

> > put the number of lines in temp into lineCount
> > if the last char of temp  is return then add one to lineCount
> 
> .. if you are creating a list you should be aware of the style 
> that you use to generate it.  In more complete applications 
> where you arerelying on user input or other data sources, the
> 'empty' last item is a real item  that needs
> to be known as 'missing' or not 'filled in' (such as a form).

Yes, I agree, but this is what I meant with software dependency. 

> One example in a stack of cards.  A inventory list is kept as 
> "one card=one product".  
> -Sort cards by product name.
> -Build a list of prices from 1 to the number of cards by putting the price
> into line x of pricesCollected and all are collected
> -then repeat with x = 1 to the number of lines in pricesCollected to find
> any missing entries.
> -If the last card has no price, 567 cards would only loop 566 times
> The last card needs to be fixed, but would not be done.
> Made a bit worse if you wanted to rank them the highest to lowest, sort
> descending would take any empty line and make it last, and a report would
> not publish the last line.

I think I see your problem, but it wouldn't happen to me, because I script slightly differently.
I would create my list pricesCollected like this:

put empty into pricesCollected 
repeat with i = 1 to (the number of cards of this stack)
	-- I don't know: Is there a "for each card" repeat loop?
	-- I'm on the wrong computer to check
	put (priceField of card i) & return after pricesCollected 
	-- usually much faster then "put into line i of pricesCollected" 
end repeat

Now pricesCollected has (number of lines) equal to the number of cards, and any missing value will appear as an empty line.
I think this is part of the xTalk philosophy: (Almost) everything is done in unstructured strings. You can think of your data as arrays (line i = a[i]), but it is much more efficient to deal with the first (or every) line and add lines at the end then accessing them individually. 

> I am doing SQL database table work and searching the tables for 
> missing and incorrect values.  I have to avoid 'sort descending'.

Again, it depends on your environment. If your SQL database gives you an additional return after each column you ask for, fine, if not, add one yourself. Similarly for lines: add a tab (or whatever) if needed. If you have the final character right, no sorting can screw up your numbers.
> 
> > put the number of lines in temp into lineCount
> > if the last char of temp  is return then add one to lineCount
> > would be more efficient and give the same result as your delimCount.
> The delimCount(string, delimiter) function can work with lines (cr) or any
> item delimeter (/ tab comma quote ^).

If you really need this, I would suggest to do it like that:

function delimCount strr, del
    put the itemDelimiter into saveDelim
    set the itemDelimiter to del
    get the number of items in strr
    if the last char of strr is del then add 1 to it
    set the itemDelimiter to saveDelim
    return it
end delimCount

This tested about 75 times faster on a list I tried. Only matters if you use this a lot, though.

All the best
Thomas




More information about the use-livecode mailing list