ForEach on array with dereferenced variable

Things you’d like to miss in the future...
Post Reply
PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

ForEach on array with dereferenced variable

Post 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 " "
Last edited by PeterH on 03 Aug 2023 16:04, edited 1 time in total.
Win11 Pro 223H2 Gerrman

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: ForEach on array with dereferenced variable

Post 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.
Win11 Pro 223H2 Gerrman

admin
Site Admin
Posts: 60633
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: ForEach on array with dereferenced variable

Post by admin »

Always on the hunt, right? :) :tup:

Both confirmed and fixed! :cup:

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: ForEach on array with dereferenced variable

Post 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!
Win11 Pro 223H2 Gerrman

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: ForEach on array with dereferenced variable

Post 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!
Win11 Pro 223H2 Gerrman

Post Reply