UDF - Access content of a variable given its name

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

UDF - Access content of a variable given its name

Post by Marco »

Here's the UDF I'm working on. In my mind, I'd like to feed in a list of variables names. The function should then read the content of each one, appending it to a new output variable. Problem: I can't access the content of said variables! Even if I prepend & to their names.
So my question is: is all this doable with an UDF?

Code: Select all

function encode_list($variables, $separator=" ") {
// Chains together the content of multiple
// variables into a single string without the
// need of separators. Such string contains a
// special header that allows extracting (single)
// variables with the companion function decode_list().
// The resulting string will have this structure:
//
// count_of_tokens;start_1-len_1#...#start_n-len_n;token_1...token_n
//
//   $variables   List of the variables names whose
//                content you want to chain together.
//                You must provide a single-quote-enclosed
//                list, like '$var_1 $var_2', otherwise
//                variables will be resolved.
//
//   $separator   Separator between variables names.
//                You should use any invalid character
//                in variables names, and of course any
//                character not used in the variables
//                names.

 $counter = 0;
 $start = 0;

 $header = "";
 $chain = "";

 foreach ($variable, "$variables", "$separator") {
  $len = strlen($variable);

  $header = $header . $start . "-" . $len . "#";
  $chain = $chain . $variable;

  $start = $start + $len;
  $counter++;
 };
 
 $header = substr("$header", 0, -1);

 return "$counter;$header;$chain";
}
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: UDF - Access content of a variable given its name

Post by highend »

This one requires you to set all variables that get passed to the function as global...

Code: Select all

    global $a = "1 a";
    global $b = "2 b";

    text encode_list('$a $b');

Code: Select all

function encode_list($variables, $separator=" ") {
// Chains together the content of multiple
// variables into a single string without the
// need of separators. Such string contains a
// special header that allows extracting (single)
// variables with the companion function decode_list().
// The resulting string will have this structure:
//
// count_of_tokens;start_1-len_1#...#start_n-len_n;token_1...token_n
//
//   $variables   List of the variables names whose
//                content you want to chain together.
//                You must provide a single-quote-enclosed
//                list, like '$var_1 $var_2', otherwise
//                variables will be resolved.
//
//   $separator   Separator between variables names.
//                You should use any invalid character
//                in variables names, and of course any
//                character not used in the variables
//                names.

    $counter = 0;
    $start = 0;

    $header = "";
    $chain = "";

    foreach ($variable, $variables, $separator) {
        global *$variable;
        $variable = *$variable;

        $len = strlen($variable);

        $header = $header . $start . "-" . $len . "#";
        $chain = $chain . $variable;

        $start = $start + $len;
        $counter++;
    }

    $header = substr("$header", 0, -1);

    return "$counter;$header;$chain";
}
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: UDF - Access content of a variable given its name

Post by bdeshi »

won't converting them to perms be a tad simpler? You just have to make sure they're unset later.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: UDF - Access content of a variable given its name

Post by Marco »

Anything less "invasive", that can be self-contained into the function?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: UDF - Access content of a variable given its name

Post by highend »

You are not passing real variables to the function, how else should they be resolved?

Imho the only other available way, expand the function to accept as many parameters as possible (iirc there is a limit)

like function encode_list($separator=" ", $var1, $var2, ...) { }

but that depends on how many vars you wanted to process...

What you'd really need is a variadic function

function encode_list($separator="", $variables*) { }

but I don't know if Don is willing to implement that...
One of my scripts helped you out? Please donate via Paypal

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: UDF - Access content of a variable given its name

Post by Marco »

highend wrote:You are not passing real variables to the function, how else should they be resolved?
Yes, I can see the flaw in my logic in hindsight.
And since I don't plan to pass more than 5 vars, I suppose I can use your approach. Thanks for the enlightenment!
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: UDF - Access content of a variable given its name

Post by bdeshi »

the limit is 11 parameters.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

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

Re: UDF - Access content of a variable given its name

Post by Filehero »

Afaiu an array would suffice to meet Marco's needs, wouldn't it? I'd love to see XY supporting arrays. :biggrin:
Last edited by Filehero on 15 Aug 2015 11:42, edited 1 time in total.

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: UDF - Access content of a variable given its name

Post by Marco »

Thanks SammaySarkar for the info! :tup:

FileHero, that's exactly what I'm trying to achieve.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

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

Re: UDF - Access content of a variable given its name

Post by Filehero »

Marco wrote:FileHero, that's exactly what I'm trying to achieve.
We've got UDFs - so there is hope. :pray:

I for one would be happy with:
- arrays
- hashs/dictionaries

This would render writing a certain type of SC tasks so much easier.

Post Reply