Page 1 of 1

Array: 0 and empty values

Posted: Sat May 11, 2019 10:14 am
by Zax
Hello,

Maybe it has been already discussed but I don't understand why 0 numeric values are modified to empty values in arrays.
Example:

Code: Select all

   put "Peter" into arr1["name"]
   put 0 into arr1["level"]
   answer arr1["name"] & cr & arr1["level"]
	 --> OK
   
  put arr1 into arr2
  answer arr2["name"] & cr & arr2["level"]
   	--> display "Peter" and an empty line !!!
Any idea?

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 10:36 am
by richmond62
I fail to see any great difference between 0 and 'empty'; equating, as they do, to the same thing.

If 'empty' offends you then I suppose you could do something like this:

Code: Select all

put arr1 into arr2
put arr2["level"] into LLEVEL
if LLEVEL is empty then put 0 into LLEVEL
  answer arr2["name"] & cr & LLEVEL

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 10:41 am
by LCMark
@zax: They aren't - if you put 0 into an array element, it stays as 0.

Indeed, I tried:

Code: Select all

put "Peter" into arr1["name"]
put 0 into arr1["level"]
answer arr1["name"] & cr & arr1["level"]
put arr1 into arr2
answer arr2["name"] & cr & arr2["level"]
In the message box in 9.0.x and it displayed the same thing both times.

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 11:26 am
by bogs
I am also curious why your seeing a different result than what you would expect. Being the lazy cuss I am, I copied your code directly from the posting to a message box, both answer dialogs came back exactly the same in 6.x to 8.x versions on 'nix.
Answer 1...
Answer 1...
Answer 2...
Answer 2...
Which OS and version are you finding something different in?

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 12:01 pm
by Zax
Ahem, I'm very embarrassed because now I've got a correct answer with 0 value :oops:
However, I did not dream. I 'll try to pay attention to my script, to see It will happen again.

Anyhow, thanks for you replies.

@richmond62
Empty is... an empty string, and 0 is an integer ;)
I know in PHP for example, false value and empty are the same, but testing I think a 0 numeric value should not be the same as an empty value.

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 1:08 pm
by richmond62
With LC 9.0.3 and MacOS 10.14.5 I didn't get an answer window!
-
Screenshot 2019-05-11 at 15.07.03.png

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 1:28 pm
by Klaus
Well, I get the answer dialog as advertized:
Peter
0
Answer1

Via message box and via a button:

Code: Select all

on mouseUp pMouseButton
   put "Peter" into arr1["name"]
   put 0 into arr1["level"]
   answer arr1["name"] & cr & arr1["level"] & CR & "Answer1"
end mouseUp
macOS 10.14.4 (10.14.5 is yet to come 8) ) and LC 9.03.

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 2:20 pm
by richmond62
macOS 10.14.4 (10.14.5 is yet to come 8) ) and LC 9.03.
-
Screenshot 2019-05-11 at 16.18.56.png
Screenshot 2019-05-11 at 16.18.56.png (38.65 KiB) Viewed 7960 times
-
Screenshot 2019-05-11 at 16.20.06.png

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 2:27 pm
by Klaus
Ah, the Bertha version, OK, I would never trust the results of testing on a beta OS sytem. :D

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 3:03 pm
by [-hh]
zax wrote:Empty is... an empty string, and 0 is an integer
In LC everything is a string.
There are situations where empty is evaluated as zero.

For example 1*empty yields zero and 1+empty yields one.

Also empty parameters of a handler/function are handled as zero:

Code: Select all

function testIt x
  return x is zero
end testIt
Then testIt() returns true.

Nothing strange, but good to know.

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 3:10 pm
by bogs
[-hh] wrote: Sat May 11, 2019 3:03 pm Nothing strange, but good to know.
Very good to know, I had assumed it was as zax wrote.

Re: Array: 0 and empty values

Posted: Sat May 11, 2019 4:48 pm
by Zax
[-hh] wrote: Sat May 11, 2019 3:03 pm Nothing strange, but good to know.
Interesting!
(I hate these internal type conversions/transformations, but I suppose that's the price to have an easy programming langage)