Jump to content
  • 0

Question

Posted (edited)

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

3 answers to this question

Recommended Posts

Posted

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

Posted

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];
}
 
Posted
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.

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...