Page 1 of 1

Build string of variables

Posted: 30 Oct 2018 23:36
by hermhart
Is it possible to build a variable depending on your choices?

I'm trying to build a variable so if I have an inputselect box that has 3 choices and I choose check box 2, I want the result to be $1*$3, if I choose checkbox 3 the result would be $1$2*, or if I choose checkboxes 1 and 2 the result would be **$3 (or perhaps a single "*" instead of 2 wildcards in a row). The purpose of this is that I have a regex of several groups of characters and I want the checkbox to result in the use of the asterisk '*' to be a wildcard wherever I choose but keep the other variables (groups).

$1*$3 would be: group1*group3
$1$2* would be: group1group2*
**$3 would be: **group3

What I have so far, but am stuck. :(

Code: Select all

       $source = inputselect("Choose an option:<crlf>(select only one) ", "1|2|3|4|5", , 2+8192, , 300, 262, Selection);

        if ($source=="1") {
          $result = "$1";
          }
       else {
          $result = "*";
          }

       if ($source=="2") {
          $result = "$result" . "$2";
          }
       else {
          $result = $result . "*";
          }

       if ($source=="3") {
          $result = "$result" . "$3";
          }
       else {
          $result = $result . "*";
          }
          

Re: Build string of variables

Posted: 31 Oct 2018 00:06
by highend
No clue why this "design" is chosen... Why exactly do you need different variables instead of storing the result in one (e.g. separating values via "|")?

Apart from that you are talking about using the dereference operator (help file!)
Some sample code:

Code: Select all

    $folders = "C:\Temp|D:\Temp";

    while ($i++ < gettoken($folders, "count", "|")) { // Two time loop
        // '$folder' is the new first part of the variable here, not a reference to an existing variable!
        // This first part must be SINGLE quoted, otherwise it could reference an already existing variable
        // "$folder", which would NOT work!
        $var   = '$folder_' . $i;
        $value = gettoken($folders, $i, "|");
        *$var  = $value;
    }
    echo $folder_2;


Re: Build string of variables

Posted: 31 Oct 2018 00:21
by hermhart
Not sure that I understand it all, but I will try to break it down and figure it out if I can.

Thank you for the help!

Re: Build string of variables

Posted: 31 Oct 2018 09:54
by PeterH
hermhart wrote: 31 Oct 2018 00:21 Not sure that I understand it all, but I will try to break it down and figure it out if I can.

Thank you for the help!
You may want to have a look at Help: Scripting / Dereference Operator :idea:

Re: Build string of variables

Posted: 02 Feb 2024 15:31
by yusef88
highend wrote: 31 Oct 2018 00:06

Code: Select all

    $folders = "C:\Temp|D:\Temp";

    while ($i++ < gettoken($folders, "count", "|")) { // Two time loop
        // '$folder' is the new first part of the variable here, not a reference to an existing variable!
        // This first part must be SINGLE quoted, otherwise it could reference an already existing variable
        // "$folder", which would NOT work!
        $var   = '$folder_' . $i;
        $value = gettoken($folders, $i, "|");
        *$var  = $value;
    }
    echo $folder_2;

Sorry for the question
how can make $folder_2, $folder_3 and so on a global variables?

Re: Build string of variables

Posted: 02 Feb 2024 15:42
by highend
By putting a global in front of *$var = $value;

But to get the value of such a variable e.g. inside a function (without passing the variable to it), you would need a global statement there as well for that specific variable => Makes no sense

For what exactly would you need such a variable as a global? Show code...

Re: Build string of variables

Posted: 02 Feb 2024 16:22
by yusef88
Actually what I need are just two variable values. I was asking out of curiosity if such thing is possible i can modify the script to meet my needs. don't bother thanks

Code: Select all

"_Initialize"
	Global $ffmpeg = "D:\master\ffmpeg-n6.0-win32-shared\bin\ffmpeg.exe";
	if ( <drop> ) { $files = "<drop |>"; }
	else { $files = "<selitems |>"; }
    while ($i++ < gettoken($files, "count", "|")) {
        $var   = '$file' . $i;
        $value = gettoken($files, $i, "|");
        *$var  = $value;}
        
"combine"
	run lax("$ffmpeg" -i "$file1" -i "$file2" -c:v copy -c:a copy "<curbase>"_mux.mkv)

"example"
	$ffmpeg $file1 $file2 $file3

Re: Build string of variables

Posted: 02 Feb 2024 19:40
by highend
The script doesn't work that way. You need to have global declarations in each subsection for each variable that you've defined as global in "_Initialize"
And if you only ever need 2 variables I see no need to use a loop and dereferencing...

Code: Select all

"_Initialize"
    Global $ffmpeg = "D:\master\ffmpeg-n6.0-win32-shared\bin\ffmpeg.exe";
    if ( <drop> ) { $files = "<drop |>"; }
    else { $files = "<selitems |>"; }
    while ($i++ < gettoken($files, "count", "|")) {
        $var   = '$file' . $i;
        $value = gettoken($files, $i, "|");
        global *$var  = $value;
    }
    // Alternative instead of the dereferencing above for two files
    global $file1 = gettoken(<selitems |>, 1, "|");
    global $file2 = gettoken(<selitems |>, 2, "|");

"combine"
    Global $ffmpeg, $file1, $file2;
    run lax("$ffmpeg" -i "$file1" -i "$file2" -c:v copy -c:a copy "<curbase>"_mux.mkv)
...
If there are really multiple file variables you could use the loop but in this case I wouldn't use a multi-section script but instead a normal one and create popupmenu() entries, eval & execute them.

Re: Build string of variables

Posted: 03 Feb 2024 11:31
by yusef88
this seems simple and easy
thank you for explaining

Code: Select all

"_Initialize"
    Global $ffmpeg = "D:\master\ffmpeg-n6.0-win32-shared\bin\ffmpeg.exe";
    global $file1 = gettoken(<selitems |>, 1, "|");
    global $file2 = gettoken(<selitems |>, 2, "|");
    
"combine"
    Global $ffmpeg, $file1, $file2;
    run lax("$ffmpeg" -i "$file1" -i "$file2" -c:v copy -c:a copy "<curbase>"_mux.mkv)
...