Page 1 of 2
Dynamically Change Input Variable - Can We...?
Posted: 19 Jun 2015 01:13
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.
Re: Dynamically Change Input Variable - Can We...?
Posted: 19 Jun 2015 08:45
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, "");
}
Re: Dynamically Change Input Variable - Can We...?
Posted: 19 Jun 2015 09:03
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?
Re: Dynamically Change Input Variable - Can We...?
Posted: 20 Jun 2015 08:36
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.
Re: Dynamically Change Input Variable - Can We...?
Posted: 20 Jun 2015 12:15
by PeterH
Sorry: I don't understand sense of this in any way
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
Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 06:59
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)

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 07:52
by SkyFrontier
Can't understand what you did, Sammay. Sorry.
Struggling with regex again...

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 08:17
by highend
Can't understand what you did, Sammay. Sorry.
Struggling with regex again...
What regex?

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 08:27
by SkyFrontier
highend wrote:Can't understand what you did, Sammay. Sorry.
Struggling with regex again...
What regex?

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.

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 08:32
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.
Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 09:29
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:\"...
Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 09:46
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...

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 10:11
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

Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 10:22
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.
Re: Dynamically Change Input Variable - Can We...?
Posted: 22 Jun 2015 10:25
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).