Page 1 of 1

ForEach on array with dereferenced variable

Posted: 03 Aug 2023 11:51
by PeterH
ForEach($tok, *$var) (i.e. for a string) works OK
ForEach(*$var, ...) (i.e. for a dereferenced array) leads to ERROR 9 | Subscript out of range.

I'm convinced the second should work - especially as the first does?

Example, for string and for array:

Code: Select all

//§ Test ForEach with DeReference

"Test ForEach DeRef : TFEDR"
   STEP;
   $var  = '$p3';      // name of a parameter
   *$var = 'this|value';

   ForEach($tok, *$var, '|')   {   // this is OK
      echo "tok = $tok";
   }

   STEP;
   $var  = '$p4';      // another parameter
   Explode(*$var, $p3, '|');   // use previous string for new arrray

   ForEach(*$var as $ind => $val) { // ERROR 9 | Subscript out of range
      echo "$ind is $val";
   }
edit: replaced some tabs by " "

Re: ForEach on array with dereferenced variable

Posted: 03 Aug 2023 16:01
by PeterH
Trying to circumvent the problem I tried to copy the (real) array before ForEach():

Code: Select all

   $p4 = an array like before
   $var = '$p4';
   $value = *$var; // ERROR: this assigns "" to $value
As before I tested a string instead of an array: no problem!

So dereference to a variable that's an array seems to generally fail.

Re: ForEach on array with dereferenced variable

Posted: 04 Aug 2023 11:52
by admin
Always on the hunt, right? :) :tup:

Both confirmed and fixed! :cup:

Re: ForEach on array with dereferenced variable

Posted: 04 Aug 2023 12:15
by PeterH
admin wrote: 04 Aug 2023 11:52 Always on the hunt, right? :) :tup:
Sorry, but no :naughty:
I really use it :biggrin:

So in this case I had no choice: had to wait for your fix to continue. No circumvention.
But no rain in the moment: first will go to the forest!

Thanks for the fix: will test later!

Re: ForEach on array with dereferenced variable

Posted: 04 Aug 2023 17:19
by PeterH
Now it's later. (Much!)

After fixing my own :bug: :bug: :bug: ( :oops: )
I could verify: this :bug: is fixed, too!

So thanks again!