Use of SC isset

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Use of SC isset

Post by klownboy »

Hi, I was under the impression that SC isset checked if a variable was actually set or established as it states in the help file. Why is it that simply declaring the variable seems to give a positive "1" return with the variable nul, empty, "". Should that be the case? Take this bad example below, I'm always getting a positive "1" return simply because the variable is declared in the beginning of the script no matter what menu item is selected. I assumed "isset" would only be true if menu item 2 is selected. Am I looking at this incorrectly or is this because it's a menu?

Code: Select all

"  Run Program 1|:refresh : THUMB_REFRESH"
    global $parentDir, $ea_path;
    $XYparentDir = "<xypath>";
    sub "_MESSAGE"; end 1;

"  Run Program 2|:refresh : THUMB_REFRESH"
    global $parentDir, $ea_path;
    $ea_path = "<xyscripts>";
    sub "_MESSAGE"; end 1;

"_MESSAGE";
    global $parentDir, $ea_path;
    if(isset($ea_path) == "1") {$test_folder = $ea_path; }
    else {$test_folder = $XYparentDir; }
    text "If Run Program 1, Variable ea_path was not set, test_folder<crlf>below should be XYpath, not XYscripts.<crlf 2>Test Folder: $test_folder";
I was going to use isset to determine which menu item was selected. I know there are other way to accomplish that, but was curious why this wasn't working.
Thanks,
Ken

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Use of SC isset

Post by TheQwerty »

Because Globals and Perms are initialized to empty strings.
::help('idh_scripting_comref.htm#idh_sc_global'); // Global SC wrote:If a variable has already been defined as global before, then the global command initializes it to its current global value, else it is initialized to an empty string.

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Use of SC isset

Post by klownboy »

Thanks TheQwerty, that's what I was expecting or fearing. I could see in stepping through the script that the global was set to an empty string, "". So if code was all self-contained within the same script and therefore there was no need to assign the variables as global or perm then this would work as I was hoping it would. Since it's a menu type script that 's not possible. So I think I'll use gettoken or gettokenindex with caller ("caption") to determine which menu selection was made. Thanks, I should have been looking at help for global as well as isset.

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Use of SC isset

Post by FluxTorpedoe »

Hi'
Back online (for a short time), so I tought I'd help a bit...

Code: Select all

if $var         »  determines if a variable is different from zero
if isset($var)  »  determines if a variable has been declared (with or without content)

---------------------------

if $var{}                   // true  — because $var = "$var" !
set $var; if $var{}         // false — because $var = 0
$var=0; if $var{}           // false — because $var = 0
$var=1; if $var{}           // true  — because $var different from 0

if isset($var){}            // false — because $var hasn't been declared
set $var; if isset($var){}  // true  — because $var has been declared
$var=0; if isset($var){}    // true  — because $var has been declared
$var=1; if isset($var){}    // true  — because $var has been declared
I looked into my scripts to find some "real life" uses of isset(), so here are two (stripped down) examples.

:arrow: isset() used to check the existence of a permanent variable:

Code: Select all

  if !isset($p_VirtualDrive) {
    action("mount drive");
    perm $p_VirtualDrive;
    $p_VirtualDrive = get("drives", 5);
  } else {
    action("unmount drive");
    unset $p_VirtualDrive;
  }
:arrow: isset() used "internally" to check if an action has been done:

Code: Select all

  foreach($CurrentItem, $list) {
    if ($CurrentItem == $PreviousItem) {
      action("Do something");
      $i++;
    }
    $PreviousItem = $CurrentItem
  }

  if isset($i) {
    msg "$i duplicate items processed";
  }
------------------------------------------------------

So in in your script, you could just use: if $ea_path {}, which would give (note: I fixed the missing "XY" in some $parentdir):

Code: Select all

"  Run Program 1|:refresh : THUMB_REFRESH"
    global $XYparentDir, $ea_path;
    $XYparentDir = "<xypath>";
    sub "_MESSAGE"; end 1;

"  Run Program 2|:refresh : THUMB_REFRESH"
    global $XYparentDir, $ea_path;
    $ea_path = "<xyscripts>";
    sub "_MESSAGE"; end 1;

"_MESSAGE";
    global $XYparentDir, $ea_path;
    if $ea_path {$test_folder = $ea_path; }
    else {$test_folder = $XYparentDir; }
    text "If Run Program 1, Variable ea_path was not set, test_folder<crlf>below should be XYpath, not XYscripts.<crlf 2>Test Folder: $test_folder";
[/size]Still, a gettoken() on the caption can be a valid alternative if you'd rather avoid using global variables for whatever reason.

Hope this helps.

Have a nice day, 8)
Flux
Last edited by FluxTorpedoe on 27 Nov 2014 17:06, edited 3 times in total.

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Use of SC isset

Post by klownboy »

Hi Flux, thanks for the examples. So the bottom line is I could have simply used "if ($var)" instead of "if(isset($var...)". Since isset will be valid or set and return a true value even when the variable is only declared (and has a null value), whereas if($var) will be true only when variable has been assigned an actual value (i.e., if it is blank or only declared, then the "if $var" would not be true). I bookmarked the post for future reference on isset.
Many thanks on this Thanksgiving Day! :cup:
Ken

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Use of SC isset

Post by FluxTorpedoe »

Hmmm, nearly there but not quite, in fact:
if $var{} is true, because an unassigned $var equals $var and not 0 !

I have just updated the beginning of my previous post with key differences between if $var and if isset($var), it should make things clearer.

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Use of SC isset

Post by klownboy »

Ok, I think I've gotcha even better now. Using if $var, as long as $val is set to a value or something other than "0", the if statement will be true or "1". If $var is set to "0", the if statement will be false, "0". However, with no declarations or setting of $var, if $var{echo $var;} will be true and echo "$var". It looks like we have to use some caution in setting or declaring/not declaring variables to arrive at the proper result in using if ($var) { } especially when globals and multi-scripts are involved.
Thanks for the further explanations Flux.
Ken

Post Reply