Dynamically Change Input Variable - Can We...?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

I've missed several updates so this may or may not be currently possible.
Please consider:

Code: Select all

   $a = "1,2,3,4,5";
 step;
   foreach ($tk, $a, ",") {
 $tst = $tk;
 $b = replace($a, 2, "");
 unset $a;
 $a = $b;
 status "next " . '$tst value CANNOT be 2!';

                            }
Is there a way to dynamically change the original $a value in ways foreach() loop gets the newly updated value?
It seems that even writing temp files wouldn't do, so I'm open to any other suggestions.
Thanks.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by highend »

Do it with a while loop

Code: Select all

$a = "1,2,3,4,5";

    $cnt = gettoken($a, "count", ",");
    while($i++ < $cnt) {
        $tst = gettoken($a, $i, ",");
        $a = replace($a, 2, "");
    }
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: Dynamically Change Input Variable - Can We...?

Post by bdeshi »

I'd also suggest a breaker and an updater so that the loop can keep up with changed tokencount.

Code: Select all

"Dynamic Foreach() loop"
 // an alternative loop to foreach that
 // can keep up with tokenlist updates.
    $a = '1,2,3,4,5';
    $s = ',';
    $c = gettoken($a, 'count', $s);
    while ($i++ < $c) {
        $c      = gettoken($a, 'count', $s);
        if  ($i > $c) { break; }
        status  gettoken($a, $i, $s);
        $a      = '1,,3,4,5,6';
        //$a    = '1,2,3';
    }
[ed]

btw, this is a great snippet. Maybe this topic should move to script exchange?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

SammaySarkar wrote: btw, this is a great snippet. Maybe this topic should move to script exchange?
I agree. It should also make into a Scripting Help topic.

Later will try to solve the problem that arose this question.
Thanks.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by PeterH »

Sorry: I don't understand sense of this in any way :shock:

As it is coded now you change the string that's been looped upon for every pass. (I.e. for every pass delete the '2' - though after the first there will be none left.) Seems nonsense? Do it once *before* the loop?

And I would never ever change the string I'm looping on. (I.e. $a.)
*If* the change would be conditional, and so would have to take place *inside* the loop, I would copy the original string to a new variable, with any appropriate change, but never touch the original.

By the way: in the original version, on start of loop, $a is copied, and the loop is working on this copy - so a change *can* have no effect. I would say this is is absolutely correct.

And to say: the Unset doesn't make any sense, too. It's same as always locking a door before opening it. After opening it it's open - no matter what it was before.

My 2 ct

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

Hi, Peter.

In case I'm trying to improve the general speed of script execution. The loop is used because I need to check a huge list of input ($a), reducing it to a minimum common value, thus allowing the magic.
The actual case is described here: http://www.xyplorer.com/xyfc/viewtopic. ... 60#p125367 , and this is an idea I had to solve it.

Feel free to contribute there, too, if you'd like to.

(now I'll finally study the suggested methods) :roll:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

Can't understand what you did, Sammay. Sorry.

Struggling with regex again...
:blackstorm:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by highend »

Can't understand what you did, Sammay. Sorry.

Struggling with regex again...
What regex? :ninja:
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

highend wrote:
Can't understand what you did, Sammay. Sorry.

Struggling with regex again...
What regex? :ninja:

Code: Select all

   $re = "";
   $a = <<<hrdc
c:\test\sub\file.txt
c:\stuff\soft\FirefoxPortable\Other
c:\stuff\soft\App
c:\stuff\soft\FirefoxPortable\FirefoxPortable.exe
c:\test\sub2\Other\prog.exe
hrdc;

   $a = formatlist($a, sde, <crlf>);
    $cnt = gettoken($a, "count", "<crlf>");
    while($i++ < $cnt) {
        $tst = gettoken($a, $i, "<crlf>");
        $tst = gettoken($tst, -2, "\", , 1);
        $tst = replace("$tst", "\", "\\");
                $a = regexreplace("$a", "$tst" . ".*$",  "$tst" . "<crlf>"); // here is the key!
                       }

   $a = regexreplace("$a", "\\{2,}", "\");
      $a = regexreplace($a, "\x0A", "");
      $a = regexreplace($a, "\x0D{1,}", "<crlf>");
   $a = formatlist($a, sde, <crlf>);
   text $a;
I'm trying to reduce the input to its common base elements c:\stuff\soft\ , c:\test\.
Bu then I realize it'll fail with c:\test\sub2\Other\prog.exe, especially.
:|
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

Important:
An input like
c:\stuff\soft\
c:\test\
>>
c:\ <- would be the expected result, but drives are NOT allowed as output, so original elements must be the output in this case.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by highend »

c:\test\sub\file.txt
c:\stuff\soft\FirefoxPortable\Other
c:\stuff\soft\App
c:\stuff\soft\FirefoxPortable\FirefoxPortable.exe
c:\test\sub2\Other\prog.exe
c:\stuff\soft\
c:\test\
Without any scripts, post the desired output of these entries (no deletion of duplicates, no sorting, etc.)

And I don't understand why "c:\stuff\soft\" should output "c:\" (which isn't desired). Your gettoken(..., -2,"\", , 1); would return "c:\stuff\soft" and not "c:\"...
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

output:

c:\stuff\soft\
c:\test\

-which is what all entries have in common.

*if* and only if
c:\stuff\soft\
c:\test\
were input, they do share C:\ in common, but this is not allowed because the goal of all of this is to redux lists of subfolders for further recursive folderreport'ing - in a manner that unnecessary processing is avoided.
Thanks in advance, highend.

edit: concerning the gettoken: it's work in progress. Didn't got there yet... :?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by highend »

The first thing to solve is the logic of how you get:
c:\stuff\soft\
c:\test\

from each <path\> | <path>\<file>

You're trying to be one step ahead^^

The rest is easy after this :)
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dynamically Change Input Variable - Can We...?

Post by SkyFrontier »

The original concept was:

-get rid of all files (paths with extensions - same old problem: how to differ a folder having dots from a legit file, keeping in mind that some extensions can have > 3 chars); worst case, get rid of everything that looks like a file)
-sort alphabetically
-get first token, match everything that's beyond it, get rid of them keeping this first token
-input trimmed, get second token and loop until the end of the list.
Last edited by SkyFrontier on 22 Jun 2015 10:41, edited 1 time in total.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Dynamically Change Input Variable - Can We...?

Post by highend »

-get rid of all files (paths with extensions - same old problem: how to differ a folder having dots from a legit file, keeping in mind that some extensions can have > 3 chars); worst case, get rid of everything that looks like a file)
There is only one way to be sure that you get the desired result sc exist and check if the result is a file or folder. No regex can make sure that it's trunkating things from a full path if there is no exact pattern (e.g. files will only have at max 3 chars and folders will NEVER have less than 3 chars).
One of my scripts helped you out? Please donate via Paypal

Post Reply