Page 1 of 1

Case of the Curious Comma

Posted: Sat Mar 05, 2016 6:40 am
by RossG
I have a routine which generates ten random numbers
and the result is as expected....

3,12,5,32,13,26,7,19,8,15,

but when I sort the items numeric ascending I get

,3,5,7,8,12,13,15,19,26,32

Is this as expected?

I thought the lack of the last comma might have been
causing another problem which I'll post if I fail to figure
it out.

Re: Case of the Curious Comma

Posted: Sat Mar 05, 2016 8:17 am
by [-hh]
Empty is, when handled as number, equivalent to zero.
And a numeric sort compares empty to other numbers.

Others do that setting also, for example spreadsheat apps as Excel for empty cells.

For example the following is in LC true:

empty+3=3
min(1,,2)=0

This doesn't matter if one is adding numbers, but one should have the second example in mind, that could be misleading.

So, in fact, for numerical uses,
... your random list ends with zero
... and the sorted list has a zero in front of it
... and the minimum of your list is zero, as long as an item is empty,

-hh

Re: Case of the Curious Comma

Posted: Sat Mar 05, 2016 8:20 am
by Simon
H Ross,
You've just open a can of worms... again :)
http://forums.livecode.com/viewtopic.php?f=7&t=22859
That's just one of the posts.

Simon

Re: Case of the Curious Comma

Posted: Sat Mar 05, 2016 8:45 am
by [-hh]
Hi Simon,
that was now a bit overcross ;-)

I think here is the "real" problem not the *last* empty item but, more general, an *empty* item. And how this is handled by LC for numerical procedures.

I think this is definetely NOT a bug here but a setting with sometimes unwanted side-effects. The last-item-and-number-of-items-discussion is ... whatever you name it. It's useless to argue there.

Recently I made an enhancement request for product(<list>). If that comes, then we have to be acquainted with the setting "one= empty item". That is we have then, logically true, (because these are different "empty"s, an empty-0 and an empty-1, as neutral elements for addition and multiplication):
empty*(empty+empty) = 1*0=0 but also empty*empty + empty*empty = 1+1=2,
what proves 0=2.

Have fun!
Hermann

Re: Case of the Curious Comma

Posted: Sat Mar 05, 2016 12:14 pm
by RossG
Thank you all for your comments.

It's easy enough to delete the first character to get
rid of the leading comma - it's just cosmetic I think -
and put a comma at the end although it's not necessary
except when I put another group of comma-delimited
numbers after the first.

No matter, it's still easier that "C".

Re: Case of the Curious Comma

Posted: Sat Mar 05, 2016 4:50 pm
by dunbarx
Hi.

All good stuff.
It's easy enough to delete the first character to get
rid of the leading comma
It is not robust to simply delete the first character. Here be pitfalls.

It takes a little more effort, but I would take the time to check each item and only delete if it empty.

Code: Select all

repeat for each item tItem in yourVar
if tItem is a number then put tItem & "," after newVar
Sounds like unnecessary work? One day you might not think so.

Craig Newman

Re: Case of the Curious Comma

Posted: Sun Mar 06, 2016 4:45 pm
by jacque
Alternately delete the last comma before doing the sort. That's shorter than a repeat loop and you know it's there if the handler created the list itself.