Array: 0 and empty values

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Array: 0 and empty values

Post 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?
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: Array: 0 and empty values

Post 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
LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1235
Joined: Thu Apr 11, 2013 11:27 am

Re: Array: 0 and empty values

Post 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.
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Array: 0 and empty values

Post 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?
Image
Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Array: 0 and empty values

Post 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.
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: Array: 0 and empty values

Post 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
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array: 0 and empty values

Post 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.
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: Array: 0 and empty values

Post 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 7955 times
-
Screenshot 2019-05-11 at 16.20.06.png
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array: 0 and empty values

Post by Klaus »

Ah, the Bertha version, OK, I would never trust the results of testing on a beta OS sytem. :D
[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Array: 0 and empty values

Post 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.
shiftLock happens
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Array: 0 and empty values

Post 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.
Image
Zax
Posts: 519
Joined: Mon May 28, 2007 10:12 am
Contact:

Re: Array: 0 and empty values

Post 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)
Post Reply