Quantcast

numberFormat affecting array keys???

classic Classic list List threaded Threaded
53 messages Options
123
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

numberFormat affecting array keys???

Mark Wieder via use-livecode
Hi all.

Put this into a button:

on mouseUp
   set the numberFormat to "00"
   repeat with i=1 to 10
      put i into myArray [i]
      breakpoint
   end repeat
end mouseUp  

At the breakpoint examine the array. There will be a 1 in an element with the name... ready??? "01"

Okay THAT has GOT to be a bug!!! Why the hell is numberformat modifying the name of the array element?? I haven't done ANY math on it. Apparently the engine DOES!

Bob S



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
Bob Sneidar wrote:

 > Hi all.
 >
 > Put this into a button:
 >
 > on mouseUp
 >    set the numberFormat to "00"
 >    repeat with i=1 to 10
 >       put i into myArray [i]
 >       breakpoint
 >    end repeat
 > end mouseUp
 >
 > At the breakpoint examine the array. There will be a 1 in an element
 > with the name... ready??? "01"
 >
 > Okay THAT has GOT to be a bug!!! Why the hell is numberformat
 > modifying the name of the array element?? I haven't done ANY math on
 > it. Apparently the engine DOES!

I believe that has to do with the evaluation done in the debugger's
variable viewer, and not in the array itself.

Why it happens I can't say, but the array itself seems fine:

If you get the keys of the array you'll see they're padded with
preceding 0s as you'd expect.  And the same is true for the values for
each key.

In fact, when I first tested this I added this just before the end of
the handler:

   put myArray[2]

...and I got empty.

Then I changed that to:

   put myArray[02]

..and I got:

  02

--
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  [hidden email]                http://www.FourthWorld.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
Array keys are strings. Even when you: put 1 into tArray[1] the "1" as
the array key is a string. If you do something mathematical to that key,
LC converts any string that represents a number to a number when it
needs to.

On 4/20/2017 7:51 PM, Bob Sneidar via use-livecode wrote:

> Hi all.
>
> Put this into a button:
>
> on mouseUp
>    set the numberFormat to "00"
>    repeat with i=1 to 10
>       put i into myArray [i]
>       breakpoint
>    end repeat
> end mouseUp  
>
> At the breakpoint examine the array. There will be a 1 in an element with the name... ready??? "01"
>
> Okay THAT has GOT to be a bug!!! Why the hell is numberformat modifying the name of the array element?? I haven't done ANY math on it. Apparently the engine DOES!
>
> Bob S
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
I would have thought that A[1] and A[01] would be the same, while A["1"] and A["01"] were different - good to know that is not the case.

Sent from my iPhone

> On Apr 20, 2017, at 8:04 PM, Richard Gaskin via use-livecode <[hidden email]> wrote:
>
> Bob Sneidar wrote:
>
> > Hi all.
> >
> > Put this into a button:
> >
> > on mouseUp
> >    set the numberFormat to "00"
> >    repeat with i=1 to 10
> >       put i into myArray [i]
> >       breakpoint
> >    end repeat
> > end mouseUp
> >
> > At the breakpoint examine the array. There will be a 1 in an element
> > with the name... ready??? "01"
> >
> > Okay THAT has GOT to be a bug!!! Why the hell is numberformat
> > modifying the name of the array element?? I haven't done ANY math on
> > it. Apparently the engine DOES!
>
> I believe that has to do with the evaluation done in the debugger's variable viewer, and not in the array itself.
>
> Why it happens I can't say, but the array itself seems fine:
>
> If you get the keys of the array you'll see they're padded with preceding 0s as you'd expect.  And the same is true for the values for each key.
>
> In fact, when I first tested this I added this just before the end of the handler:
>
>  put myArray[2]
>
> ...and I got empty.
>
> Then I changed that to:
>
>  put myArray[02]
>
> ..and I got:
>
> 02
>
> --
> Richard Gaskin
> Fourth World Systems
> Software Design and Development for the Desktop, Mobile, and the Web
> ____________________________________________________________________
> [hidden email]                http://www.FourthWorld.com
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
> Bob S. wrote:
> Okay THAT has GOT to be a bug!!! Why the hell is numberformat modifying the
> name of the array element?? I haven't done ANY math on it.

"repeat with i=1 to 10" uses i to count from 1 up to 10 step 1: That's a lot
of math you've done to i.

Whereas this doesn't touch the numbers:

  set the numberFormat to "00"
  put 1 into myArray[1]
  put 2 into myArray[2]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
> Bob S. wrote:
> Okay THAT has GOT to be a bug!!! Why the hell is numberformat modifying the
> name of the array element?? I haven't done ANY math on it.

"repeat with i=1 to 10" uses i to count from 1 up to 10 step 1: That's a lot
of math you've done to i.

Whereas this doesn't touch the numbers:

  set the numberFormat to "00"
  put 1 into myArray[1]
  put 2 into myArray[2]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
On 2017-04-21 01:51, Bob Sneidar via use-livecode wrote:

> Put this into a button:
>
> on mouseUp
>    set the numberFormat to "00"
>    repeat with i=1 to 10
>       put i into myArray [i]
>       breakpoint
>    end repeat
> end mouseUp
>
> At the breakpoint examine the array. There will be a 1 in an element
> with the name... ready??? "01"
>
> Okay THAT has GOT to be a bug!!! Why the hell is numberformat
> modifying the name of the array element?? I haven't done ANY math on
> it. Apparently the engine DOES!

As others have already said, this isn't a bug - just a consequence of
the rules previously mentioned. The engine is doing this:

   repeat with i = strToNum(1) to strToNum(10)
     put i into myArray[numToStr(i)]
   end repeat

So, the value of the key is put into the array as a number but array
keys are always strings, so it is converted to a string before indexing
the array.

I think this is perhaps more evidence that 'numberFormat' should
probably
be deprecated - or at least marked as 'present for HyperCard
compatibility
only and shouldn't be used in new code'.

Indeed, even the latter is somewhat misleading as (due to the IEEE
double
internal representation used for numbers in LiveCode), numberFormat does
not and cannot work the same.

(The reason it cannot work the same is that the numberFormat expresses
precision in decimal places, but doubles use binary representation - so
a double can only approximate a decimal number to a certain number of
decimal places, it can't faithfully represent it except for a subset
of values).

My general recommendation is:

   1) Don't use numberFormat in new code.

   2) Use format() to format your numbers *when you want to display
them*.

   3) Use round() to control the precision of your numbers.

Warmest Regards,

Mark.

--
Mark Waddingham ~ [hidden email] ~ http://www.livecode.com/
LiveCode: Everyone can create apps

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
Now I have you! It's NOT doing math in the loop! I know this because the VALUE is 1 and NOT 01.

Bob S


On Apr 20, 2017, at 17:32 , hh via use-livecode <[hidden email]<mailto:[hidden email]>> wrote:

Bob S. wrote:
Okay THAT has GOT to be a bug!!! Why the hell is numberformat modifying the
name of the array element?? I haven't done ANY math on it.

"repeat with i=1 to 10" uses i to count from 1 up to 10 step 1: That's a lot
of math you've done to i.

Whereas this doesn't touch the numbers:

 set the numberFormat to "00"
 put 1 into myArray[1]
 put 2 into myArray[2]

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
Again, the VALUE is not being converted by numberFormat, so it ISN'T the LOOP calculation that is doing it! Otherwise the VALUE would ALSO be 1! I loop is using i and it clearly contains 1 and not 01 because that is what shows up in the value. I could put it the otherway. I could say I expected the VALUE to ALSO be 1 because this is how numberFormat shoud work, and it isn't.

But I agree numberFormat should be deprecated. Time to work on that formatNumber function!

Bob S


On Apr 21, 2017, at 24:26 , Mark Waddingham via use-livecode <[hidden email]<mailto:[hidden email]>> wrote:

As others have already said, this isn't a bug - just a consequence of
the rules previously mentioned. The engine is doing this:

 repeat with i = strToNum(1) to strToNum(10)
   put i into myArray[numToStr(i)]
 end repeat

So, the value of the key is put into the array as a number but array
keys are always strings, so it is converted to a string before indexing
the array.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
I got that wrong. I could say I expected the value to be 01. Also if you see  the value of i in the debugger, it indicates that the value of i is 1 and NOT 01. So it HAS to be the array designation that is doing it.

Bob S


On Apr 21, 2017, at 07:31 , Bob Sneidar via use-livecode <[hidden email]<mailto:[hidden email]>> wrote:

I could put it the otherway. I could say I expected the VALUE to ALSO be 1 because this is how numberFormat shoud work, and it isn't.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
On 2017-04-21 16:33, Bob Sneidar via use-livecode wrote:
> I got that wrong. I could say I expected the value to be 01. Also if
> you see  the value of i in the debugger, it indicates that the value
> of i is 1 and NOT 01. So it HAS to be the array designation that is
> doing it.

Yes - i is a number (as it is the index of the for loop) so the value
of the array element is a number, and so appears as '1' (not affected
by numberFormat).

However, array keys are *always* strings, so when you set the array key
numberFormat comes into play and the key is '01' and not '1'.

Warmest Regards,

Mark.

--
Mark Waddingham ~ [hidden email] ~ http://www.livecode.com/
LiveCode: Everyone can create apps

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
OIC. NumberFormat is a STRING function. So then...:
set the numberFormat to "00"; put 1+1 into tValue; put tValue produces "02" because livecode converts EVERYTHING to a string before outputting it. It makes more sense I suppose to put it that way. I have gotten into the habit of thinking that numbers are numbers because it happens so seamlessly.

It really doesn't matter though, because the bottom line is that I need to create my own number formatting function, which is no big deal. If it's any good I'll post it for review and refinement.

Bob S


> On Apr 21, 2017, at 08:18 , Mark Waddingham via use-livecode <[hidden email]> wrote:
>
> Yes - i is a number (as it is the index of the for loop) so the value
> of the array element is a number, and so appears as '1' (not affected
> by numberFormat).
>
> However, array keys are *always* strings, so when you set the array key
> numberFormat comes into play and the key is '01' and not '1'.
>
> Warmest Regards,
>
> Mark.
>
> --
> Mark Waddingham ~ [hidden email] ~ http://www.livecode.com/
> LiveCode: Everyone can create apps


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
Okay simple solution:

on mouseUp
   repeat with i = 1 to 10
      put formatNumber(i) into myArray [i]
   end repeat
   breakpoint
end mouseUp

function formatNumber pSourceString, pFormat
   set the numberFormat to pFormat
   add 0 to pSourceString
   return pSourceString
end formatNumber

This works because numberFormat gets reset once the handler exits. DOH!

Bob S



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
On 4/21/2017 11:34 AM, Bob Sneidar via use-livecode wrote:
> It really doesn't matter though, because the bottom line is that I need to create my own number formatting function, which is no big deal. If it's any good I'll post it for review and refinement.

Why not use the existing format(baseString[,valuesList]) function
instead of scripting you own?



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
Sorry. Change that to:
put formatNumber(i, "00") into myArray [i]
and it does NOT work. adding 0 to the value with numberFormat set to "00" does NOT format the number. SHEESH!

Working on it.

Bob S


> On Apr 21, 2017, at 08:58 , Bob Sneidar via use-livecode <[hidden email]> wrote:
>
> Okay simple solution:
>
> on mouseUp
>   repeat with i = 1 to 10
>      put formatNumber(i) into myArray [i]
>   end repeat
>   breakpoint
> end mouseUp
>
> function formatNumber pSourceString, pFormat
>   set the numberFormat to pFormat
>   add 0 to pSourceString
>   return pSourceString
> end formatNumber
>
> This works because numberFormat gets reset once the handler exits. DOH!
>
> Bob S
>
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
I could, but I will have to figure out how to convert a numberFormat string like "#0.00" to a format incantation, as the dictionary puts it. The goal is to convert ANY number to ANY numeric format, not just solve this one little issue which I could just code around if I wanted. This sort of thing makes my head hurt, but I'll give it a go just for the mental exercise.

Bob S


> On Apr 21, 2017, at 09:01 , Paul Dupuis via use-livecode <[hidden email]> wrote:
>
>> It really doesn't matter though, because the bottom line is that I need to create my own number formatting function, which is no big deal. If it's any good I'll post it for review and refinement.
>
> Why not use the existing format(baseString[,valuesList]) function
> instead of scripting you own?


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
In reply to this post by Mark Wieder via use-livecode
Here's an interesting case I ran into:  long time - hours can be single
digits, e.g. "8", but minutes and seconds are always two digits, e.g. "00",
and, of course, setting the numberFormat does not affect anything.
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
Okay this is why they call me Sly Dawg. I used the engine quirk against it to produce this:

on mouseUp
   repeat with i = 1 to 10
      put formatNumber(i, "00") into myArray [i]
   end repeat
   breakpoint
end mouseUp

function formatNumber pSourceString, pFormat
   set the numberFormat to pFormat
   put pSourceString into aArray [pSourceString]
   put the keys of aArray into tResult
   return tResult
end formatNumber

Booyah!

Bob S
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
Alternately, put a space after pSourceString to force it to text, which
will apply numberformat, and then return word 1 of it.

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com



On April 21, 2017 11:31:36 AM Bob Sneidar via use-livecode
<[hidden email]> wrote:

> Okay this is why they call me Sly Dawg. I used the engine quirk against it
> to produce this:
>
> on mouseUp
>    repeat with i = 1 to 10
>       put formatNumber(i, "00") into myArray [i]
>    end repeat
>    breakpoint
> end mouseUp
>
> function formatNumber pSourceString, pFormat
>    set the numberFormat to pFormat
>    put pSourceString into aArray [pSourceString]
>    put the keys of aArray into tResult
>    return tResult
> end formatNumber
>
> Booyah!
>
> Bob S
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: numberFormat affecting array keys???

Mark Wieder via use-livecode
Sly Kitty. :-)

Bob S


> On Apr 21, 2017, at 09:53 , J. Landman Gay via use-livecode <[hidden email]> wrote:
>
> Alternately, put a space after pSourceString to force it to text, which will apply numberformat, and then return word 1 of it.
>
> --
> Jacqueline Landman Gay         |     [hidden email]
> HyperActive Software           |     http://www.hyperactivesw.com


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
123
Loading...