Page 1 of 1

UDF - Access content of a variable given its name

Posted: 14 Aug 2015 11:27
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";
}

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

Posted: 14 Aug 2015 12:06
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";
}

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

Posted: 14 Aug 2015 13:40
by bdeshi
won't converting them to perms be a tad simpler? You just have to make sure they're unset later.

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

Posted: 14 Aug 2015 17:44
by Marco
Anything less "invasive", that can be self-contained into the function?

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

Posted: 14 Aug 2015 17:58
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...

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

Posted: 14 Aug 2015 23:11
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!

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

Posted: 15 Aug 2015 05:46
by bdeshi
the limit is 11 parameters.

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

Posted: 15 Aug 2015 11:27
by Filehero
Afaiu an array would suffice to meet Marco's needs, wouldn't it? I'd love to see XY supporting arrays. :biggrin:

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

Posted: 15 Aug 2015 11:37
by Marco
Thanks SammaySarkar for the info! :tup:

FileHero, that's exactly what I'm trying to achieve.

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

Posted: 15 Aug 2015 11:45
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.