Jump to content
  • 0

Null == 0?


Antares

Question


  • Group:  Members
  • Topic Count:  74
  • Topics Per Day:  0.02
  • Content Count:  420
  • Reputation:   89
  • Joined:  01/30/12
  • Last Seen:  

Hi!

 

I read the guide on scripting and this made me think:

 

if (foo) ...        // this will check if 'foo' has been declared or not. If the value 'foo'
            // holds is different from 0 (declared), it will return true. Otherwise it
            // will return false.

if (foo != 0) ...    // Same as above

 

Does this mean that a varable with 0 is the same as null (variable not declared)?

 

I need to store the index of an array, that can be any number between (incl) 0 and 19.

 

I have checks not to execute the array indexing, if the index is not set [ if ($var != null) ]

 

Do I have to rewrite my index variable to store numbers between 1 and 20 and when indexing the array reduce it by one?

Edited by Antares
Link to comment
Share on other sites

3 answers to this question

Recommended Posts


  • Group:  Forum Moderator
  • Topic Count:  93
  • Topics Per Day:  0.02
  • Content Count:  10018
  • Reputation:   2371
  • Joined:  10/28/11
  • Last Seen:  

any integer variable with value of 0 is same as "not declared" ...

I have checks not to execute the array indexing, if the index is not set [ if ($var != null) ]

basically you can compare it like this....the "null" will be treat as the "character variable" with a value of 0 ( if it's not declared ) ...

 

Do I have to rewrite my index variable to store numbers between 1 and 20 and when indexing the array reduce it by one?

you mean delete the value from array ?

you can use deletearray

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  74
  • Topics Per Day:  0.02
  • Content Count:  420
  • Reputation:   89
  • Joined:  01/30/12
  • Last Seen:  

In case of random returning 0 which one will display "a"?

 

 

 

setarray .myarray[0], "a", "b", "c", "d", "e";
$foo = rand(0,4);
if($foo){
     mes .myarray[$foo];
}
 

 

 

or 

 

 

 

setarray .myarray[0], "a", "b", "c", "d", "e";
$foo = rand(0,4) + 1;
if($foo){
     mes .myarray[$foo - 1];
}
 
Link to comment
Share on other sites


  • Group:  Forum Moderator
  • Topic Count:  93
  • Topics Per Day:  0.02
  • Content Count:  10018
  • Reputation:   2371
  • Joined:  10/28/11
  • Last Seen:  

setarray .myarray$[0], "a", "b", "c", "d", "e";
set .myarray_size,getarraysize( .myarray$ );
// $foo = rand( .myarray_size );

for( .@i = 0; .@i < .myarray_size; .@i++ )
	mes ".myarray$["+.@i+"] = "+.myarray$[.@i];
close;

 

in your case...

setarray .myarray[0], "a", "b", "c", "d", "e";
$foo = rand(0,4) + 1;
if($foo){
     mes .myarray[$foo - 1];
}

 

anyway....you dont have to check if the $foo is assigned a value or not..the array start from 0 ~ 4 ...

it will alway return the 1st value in array if your $foo isnt assigned with any value.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...