Page 1 of 2

[FIXED] Bug with Dereference Operator

Posted: 26 Jun 2014 11:54
by PeterH
Edit: please see the corrected and expanded example in my post of 28 Jun 2014!

I hope this can be seen as a :bug:

Code: Select all

 $str = '$str'.1;
 Global *$str = 1;
works as expected.

In another script, used as a subroutine, I want to use the already defined contents of $n, so:

Code: Select all

 $str = '$str'.1;
 Global *$str;
But: impossible! The Global *without* initialization results in error msg:
  • Not a valid variable name: *$str
    A variable must begin with $, followed by a name.
    The script is terminated now.
    global
    *$str
The same problem for Perm.

As Global and Perm are used to share values, it wouldn't make sense always to initialize them when defining, and lose the current contents. So I'm convinced it's a :bug:

By the way: would it be possible to automatically add the $ to the dereference name, if it's missing? I think it's unique. Then creating the name would be much easyer - example:

Code: Select all

 $nr = 2;    // maybe a paramter :biggrin: 
 $str = "str$nr"; // to address $str2 - easy if you must not code $str here!
 Global *$str;
(Usually my procedures have some more parameters - then it really makes a difference...)

And another "by the way": nobody else uses this? It seems to have a lot of power 8)

Re: Bug with Dereference Operator

Posted: 26 Jun 2014 12:48
by Marco
PeterH wrote:And another "by the way": nobody else uses this? It seems to have a lot of power 8)
I used it just the other day for the first time. It is powerful indeed, I just need to get familiar with. I can see it basically allows you to create arrays, but in a "weird" way to my mind.
Anyway, here's my code: it creates a list of variables containing the dates DST begins and ends in Europe since 1999. Each variable contains, in its name, the year. I'm pretty sure this is not possible without the deference operator (even eval is useless)

Code: Select all

 $annum = 1999;

 while ($annum <= now("yyyy")) {
  $begin = "$dst_b_"."$annum";
  *$begin = "$annum-03-".(31 - ((floor((5 * $annum) / 4) + 4) % 7));			//when European DST begins, in ISO 8601 - https://en.wikipedia.org/wiki/European_Summer_Time#Exact_timing_for_the_next_several_years
  $end = "$dst_e_"."$annum";
  *$end = "$annum-10-".(31 - ((floor((5 * $annum) / 4) + 1) % 7));			//when European DST ends, in ISO 8601 - https://en.wikipedia.org/wiki/European_Summer_Time#Exact_timing_for_the_next_several_years

  $annum++;
 };


Re: Bug with Dereference Operator

Posted: 26 Jun 2014 13:27
by TheQwerty
I experimented with using it to improve parameter passing between scripts, but it just left me wishing for true user functions.


I found my way around the invalid name issue with the following...

Code: Select all

// Helper scripts to demonstrate different $msg values.
"Greet World"
  Global $msg = 'world';
  Sub '_main';

"Greet Jane"
  Global $msg = 'Jane';
  Sub '_main';

// Main script which shows the values before and after calling _greet.
"_main"
  // This only works if the variable is a global.
  Global $msg;
  $originalMsg = $msg;

  // Calling '_greet' with derefed input.
  Global $io = '$msg';
  Sub '_greet';

  // Show results of '_greet'.
  Echo "Before Greet: '$originalMsg'<crlf>After Greet: '$msg'";

// Generates an appropriate greeting.
"_greet"
  // Name of Global Variable to modify.
  Global $io;

  // We cannot bring global var '$msg' into the local scope without its name.
  // i.e. eval("Global $io;") won't work.
  // So instead we transfer its value to a global var whose name we know.
  Load 'Global ' . $io . ';Global $temp = ' . $io . ';',,'s';

  // ^ that helps to show what is and is not resolved.
  // Personally, I find that difficult to read and thus prefer:
  // Load "Global $io;Global $temp = $io;",, 's';
  // But this will only work if $temp does not exist in the local scope.

  // Bring $temp into the local scope.
  // Doing so after the load allows for that above "trick".
  Global $temp;

  // Just a demonstration of working with the value.
  $punctuation = $temp LikeI 'world' ? '!' : '.';

  // Modify the dereferenced variable.
  Global *$io = "Hello $temp$punctuation";

  // Clean up our temporary variable to not pollute the global space.
  Unset $temp;
That's mostly comments so in practice it looks more like:

Code: Select all

"Main"
  Global $msg = $value;
  Global *$io = '$msg';
  Sub '_sub';
  Echo $msg;

"_sub"
  Global $io;
  Load "Global $io;Global $temp = $io;",, 's';
  Global $temp;
  Global *$io = "The value of $io was originally $temp.";
  Unset $temp;

Re: Bug with Dereference Operator

Posted: 26 Jun 2014 13:30
by PeterH
Marco wrote:
PeterH wrote:And another "by the way": nobody else uses this? It seems to have a lot of power 8)
I used it just the other day for the first time. It is powerful indeed, I just need to get familiar with. I can see it basically allows you to create arrays, but in a "weird" way to my mind.
Anyway, here's my code: it creates a list of variables containing the dates DST begins and ends in Europe since 1999. Each variable contains, in its name, the year. I'm pretty sure this is not possible without the deference operator (even eval is useless)

Code: Select all

 $annum = 1999;

 while ($annum <= now("yyyy")) {
  $begin = "$dst_b_"."$annum";
  *$begin = "$annum-03-".(31 - ((floor((5 * $annum) / 4) + 4) % 7));			//when European DST begins, in ISO 8601 - https://en.wikipedia.org/wiki/European_Summer_Time#Exact_timing_for_the_next_several_years
  $end = "$dst_e_"."$annum";
  *$end = "$annum-10-".(31 - ((floor((5 * $annum) / 4) + 1) % 7));			//when European DST ends, in ISO 8601 - https://en.wikipedia.org/wiki/European_Summer_Time#Exact_timing_for_the_next_several_years

  $annum++;
 };

Hi Marco,
did you do this at will? If, by chance, you used a variable $dst_b_ before execution of
$begin = "$dst_b_"."$annum";
it will fail miserably. So you should change it to
$begin = '$dst_b_'."$annum";
Here it will be treated as a string - may it be defined or not.

And thats the reason for my additional request: if scripting would be expanded this way you could simply write
$begin = "dst_b_$annum";

When and how to use it - I'm just looking
Idea: I have 2 (or n) sets of variables, all ending on 1 or 2
I need a Subroutine to work on one or the other set.
Solution: a global var is set to 1 or 2 to define the set, all variables of both sets are global.
The subroutine constructs the names of all current variables dependent from the global number - and so can address just the wanted set.

In the moment I have 2 routines - one with names for set1, the other for set2. :shock:
I could have used one set for comunication with the subroutine, and fill it before each call. But I felt this was just too much code for every single call...

Re: Bug with Dereference Operator

Posted: 26 Jun 2014 16:09
by Marco
If, by chance, you used a variable $dst_b_ before execution of
$begin = "$dst_b_"."$annum";
it will fail miserably. So you should change it to
$begin = '$dst_b_'."$annum";
Here it will be treated as a string - may it be defined or not.
Thank you, didn't know that difference between single and double quotes.

While my personal opinion is that keeping the $ helps differentiating between actual variable names and variable values, the enhancement you propose streamlines certain constructs. For scripters who are familiar with deferencing it is a plus for sure, so I support your idea, if Don says it's possible and doesn't generate syntax/parsing ambiguities.

Re: Bug with Dereference Operator

Posted: 28 Jun 2014 13:42
by PeterH
Sorry to say: on reducing the examples in the first post to the simplest form I made some ( :oops: ) mistakes :cry:

So here the complete thing in 1 piece of code, still made simpler, but correct and also showing how I generally want to use it:

Code: Select all

"Main"
 Global $ind;
   $ind = 2;
   $str = '$str'.$ind;
   Global *$str = "Some text";
   Sub '_sub';
   $txt = Eval($str);
   Text "txt = '$txt'";
// ...

"_sub"
 Global $ind;
   $str = '$str'.$ind;
   Global *$str;    // this is wanted (Skip in step-mode)
   Global  $str2;   // to simulate
   $txt = Eval($str);
   Text "txt = '$txt'";
   *$str = "Changed!"
// ...
It fails in _sub at the "Global *$str".
If you comment this one Global instruction, the next (being the resolved version of the previous) shows what *should* happen.


Still have to test with TheQwerty's example - but it's a unconvenient solution, as I have several of these variables, and I would have to indirectly handle each of them - quite a lot of extra code to make it work. And I'm afraid I wouldn't understand it after a month...

Re: Bug with Dereference Operator

Posted: 28 Jun 2014 15:02
by bdeshi
You want to declare a deferenced variable without setting a value for it.
+1 from me, for consistency in scripting commands.
Because SC set can already do it: set *$str; will work, without values :?

Re: Bug with Dereference Operator

Posted: 28 Jun 2014 22:47
by PeterH
SammaySarkar wrote:You want to declare a deferenced variable without setting a value for it.
+1 from me, for consistency in scripting commands.
Because SC set can already do it: set *$str; will work, without values :?
Help says for Dereference Operator:
You can use the dereference operator * (asterisk) on the left side of set, perm, and
global statements. Usage: Helpful when dynamically creating variables.


So I just expected Global/Perm to work without an initialization value, and so I think it's a :bug:
:lol: I just see: Unset also works without initialization :lol:


And I want to remind of my wish mentioned before: it would be *very* helpful if the leading $ of the contained variable would be allowed to miss the leading $:

Code: Select all

   $str = '$str'.$ind;   // current syntax
   $str = "str$ind";     // would be easyer if allowed
   
   $str = '$str'.1;      // leads to error: dot seems to be decimal?
   $str = '$str'. 1;     // currently correct 
   $str = "str1";        // would be fine and simple, too (no matter if single or double quotes)
You could argument: the *$ already says that's a variable name included - it must not be qualified with an extra $ :roll:

Re: Bug with Dereference Operator

Posted: 29 Jun 2014 00:58
by PeterH
TheQwerty wrote:I experimented with using it to improve parameter passing between scripts, but it just left me wishing for true user functions.

...
I found my way around the invalid name issue with the following...That's mostly comments so in practice it looks more like:

Code: Select all

"Main"
  Global $msg = $value;
  Global *$io = '$msg';
  Sub '_sub';
  Echo $msg;

"_sub"
  Global $io;
  Load "Global $io;Global $temp = $io;",, 's';
  Global $temp;
  Global *$io = "The value of $io was originally $temp.";
  Unset $temp;
This code shows that you are very creative :-)

I've managed to use it as a base for a testscript like the one I've shown in my previous post - but with 2 "dereferenced variables". Yes: it can be done. :biggrin: But I'd say the result is "rather unmanagable". :P
- It's double the size (15 instead of 8 lines),
- with some "temporary global variables" for the Load,
- the same original variable is to be addressed by different names for read and write [$temp / *$io],
- the embedded Load is rather delicate (I would put the $temp in single quotes, then it could be defined global together with $io *before* the Load instruction - but it would mean some concatination inside the load, ...)
- "misuse" of Global for Set

In other words: it really can be done - but I think it's unusable for normal situations. So I'm hoping Don changes this...

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 12:56
by admin
Sorry, no time for this.

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 13:29
by PeterH
admin wrote:Sorry, no time for this.
No time for a bug? :shock:

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 14:02
by admin
Describe the bug please.

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 14:09
by PeterH

Code: Select all

 $str = '$str'.1;
 Global *$str;
results in error msg:
  • Not a valid variable name: *$str
    A variable must begin with $, followed by a name.
    The script is terminated now.
    global
    *$str
The same problem for Perm.

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 14:16
by admin
That's a wish.

Re: Bug with Dereference Operator

Posted: 30 Jun 2014 14:25
by PeterH
Help says for Dereference Operator:
You can use the dereference operator * (asterisk) on the left side of set, perm, and
global statements. Usage: Helpful when dynamically creating variables.


So I just expected Global/Perm to work without an initialization value, and so I think it's a :bug: