[SC] Variable name contain <var>

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: [SC] Variable name contain <var>

Post by Filehero »

Hi,

I build up a set of "growing" variables dynamically in a loop at runtime using the dereference operator (because I use the variable names also as keys to .ini file values), something like that:

Code: Select all

  $ALL_KEYS = "distance|duration|average_pace|total_height|trackpoints"; 
  foreach ($key, $ALL_KEYS) {
    $var = "$".$key;
    // collect raw data per section
    $value = getkey($key, $label, $tfo_file);
    if ($value != "t.b.d.") {
      *$var = eval($var) . "$value|";
    }
  }
  *$var = trim(eval($var), "|");
note: the vars got initialized to "" in an upstream loop

Now I wonder, why this works

Code: Select all

msg("$var: " . eval($var), "1");
but this doesn't

Code: Select all

msg("$var: " . *$var, "1");
I don't get it - especially because each var is eval'ed at the end of it's corresponding write access loop (last statement in 1st code block).

Is it just syntactic incompleteness or a basic logical misunderstanding on my side?


Cheers,
Filehero

PS: :ball:

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: [SC] Variable name contain <var>

Post by PeterH »

I don't really understand what you want to do. So made a test...

No loop, no file-read, just some statements:

Code: Select all

 $Name = ""; // init empty

 $key = "Name";   // a var name
 $var = "$".$key; // prefix $
 $value = "Wert"; // assume a value
 *$var = eval($var) . "$value|"; // concatenate

 *$var = trim(eval($var), "|");  // cleanup

 msg("$var: " .eval($var), "1");
 msg("$var: " . *$var, "1");
This shows what I'd expect to happen.

And: I think eval($var) is identical to *$var
(And "1" is identical to 1)

But maybe I missed something?

Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: [SC] Variable name contain <var>

Post by Filehero »

Hi Peter
PeterH wrote:....This shows what I'd expect to happen.

And: I think eval($var) is identical to *$var
(And "1" is identical to 1)

But maybe I missed something?
I also consider both statments to be equivalent and expect them both to show
eval_var.png
However, for

Code: Select all

  msg("$var: " . *$var, "1");
I get
deref_var.png
This confuses me. But maybe the statements simply aren't equivalent?


Cheers,
Filehero
To see the attached files, you need to log into the forum.

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

Re: [SC] Variable name contain <var>

Post by admin »

Indeed, a bug! :bug: Fix comes.

Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: [SC] Variable name contain <var>

Post by Filehero »

admin wrote:Indeed, a bug! :bug: Fix comes.
Fixed, thanks.

Btw, one of the rare times where I welcomed a bug, because otherwise it had proven I still hadn't got the dereference thingie. :)


Cheers,
Filehero

Post Reply