Page 1 of 2
script to select files, folders in groups
Posted: 29 Apr 2016 16:01
by tiago
[split from User Functions Exchange]
Can I request a function here?
I'm having a hard time to code a script which could select 'files', 'folders', 'both' on groups of "x" with "y" skips
Like: select 'folders'
x = 3
y = 2
sel
file-not counted as skip
sel
sel
skip
skip
file
sel
sel
file
sel
skip
file
skip
Re: User Functions Exchange
Posted: 29 Apr 2016 16:55
by highend
I don't think that you'll learn a lot from this one if you don't show what you've got so far...
Edit: Small modification for the negation of the $types checking line -> less indentation
Renaming variables...
Code: Select all
SelectItemsInGroups("b", 3, 2); // b = both, f = files, d = directories
function SelectItemsInGroups($types, $selSize, $skipSize) {
$matches = "";
$curSelSize = 0;
$curSkipSize = 0;
foreach($item, listpane()) {
$type = exists($item);
if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
if ($curSelSize == $selSize) {
if ($curSkipSize == $skipSize) {
$curSelSize = 1;
$curSkipSize = 0;
$matches = $matches . $item . "<crlf>";
} else { $curSkipSize++; }
} else {
$curSelSize++;
$matches = $matches . $item . "<crlf>";
}
}
selectitems $matches;
}
Re: User Functions Exchange
Posted: 30 Apr 2016 19:39
by tiago
I tried. That's why I asked for the 'get' option with no great improvement on this issue. Nothing really useful.
Is it safe to mess with 'selectitems $matches;', pre-processing it via gettoken so a predefined max number of items is selected?
Thank you very much...
edit: not. I couldn't make it a proper function, then.

Re: User Functions Exchange
Posted: 30 Apr 2016 19:44
by highend
Add a parameter $limit to the function and do it in the foreach loop. Why should a script e.g. process 1000 items when your limit is e.g. 50. Or calculate a ratio $allItems / $limit and if it's low, use gettoken on $matches and if it's high, break the function once the limit is reached.
Without seeing what you coded, there is nothing to comment about...
As an advanced programmer you can learn a lot of other's code. But as a novice it isn't that helpful. In 90% of the cases you aren't able to recreate it after reading it in the first place...
Re: User Functions Exchange
Posted: 30 Apr 2016 19:57
by tiago
wait...
is this wrong?
Code: Select all
SelectItemsInGroups("d", 3, 2, 50); // b = both, f = files, d = directories
function SelectItemsInGroups($types, $selSize, $skipSize, $max) {
$matches = "";
$curSelSize = 0;
$curSkipSize = 0;
foreach($item, listpane()) {
$type = exists($item);
if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
if ($curSelSize == $selSize) {
if ($curSkipSize == $skipSize) {
$curSelSize = 1;
$curSkipSize = 0;
$matches = $matches . $item . "<crlf>";
} else { $curSkipSize++; }
} else {
$curSelSize++;
$matches = $matches . $item . "<crlf>";
}
}
$matches = gettoken($matches, $max, "<crlf>", , 1);
selectitems $matches;
}
Initial test passed ok.
What's your opinion?
Re: User Functions Exchange
Posted: 30 Apr 2016 20:02
by tiago
highend wrote:Why should a script e.g. process 1000 items when your limit is e.g. 50.
Yes, I see your point now...
Re: User Functions Exchange
Posted: 30 Apr 2016 20:04
by highend
Would work but as I already said, it's bad when you have a low limit and a large number of items...
Re: User Functions Exchange
Posted: 30 Apr 2016 20:11
by tiago
What about this...?
Code: Select all
SelectItemsInGroups("d", 3, 2, 20); // b = both, f = files, d = directories
function SelectItemsInGroups($types, $selSize, $skipSize, $max) {
$matches = "";
$curSelSize = 0;
$curSkipSize = 0;
foreach($item, listpane()) {
$type = exists($item);
if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
if ($curSelSize == $selSize) {
if ($curSkipSize == $skipSize) {
$curSelSize = 1;
$curSkipSize = 0;
$maxC++; if ($maxC == $max +1) { break; } elseif { }
$matches = $matches . $item . "<crlf>";
} else { $curSkipSize++; }
} else {
$maxC++; if ($maxC == $max +1) { break; } elseif { }
$curSelSize++;
$matches = $matches . $item . "<crlf>";
}
}
// $matches = gettoken($matches, $max, "<crlf>", , 1);
selectitems $matches;
#1012; // move into view
}
Re: User Functions Exchange
Posted: 30 Apr 2016 20:16
by highend
Too complicated. A single check on top of the foreach loop would be enough. And you don't need to add the +1 at that place. The incrementation must (ofc) still take place in their current places. If there is no elseif(), don't write it^^. Do not write multiple statements in one line. Allowed? Yes. Readable? Barely. Multiple assignments, ok, but not a combination with anything else.
Re: User Functions Exchange
Posted: 30 Apr 2016 20:26
by tiago
I'm breaking the script with attempts of single check.
Re: User Functions Exchange
Posted: 16 May 2016 21:43
by tiago
SammaySarkar wrote:allstrpos(haystack, needle, start, matchcase, rel) : Returns all positions of a substring, separated by pipe.
The Return is similar to regular strpos(), but for negative start position. Details in function comment.
...
Hiya, SammaySarkar!
Can you please provide a version in which whole words can be used in haystacks? Having much trouble on this. I'd swear strpos would do it but unfortunately it doesnt.
@highend:
What would it be your solution for SelectItemsInGroups then? Couldn't simplify it per your instructions. Sorry...
Re: User Functions Exchange
Posted: 16 May 2016 21:56
by highend
Can you please provide a version in which whole words can be used in haystacks? Having much trouble on this. I'd swear strpos would do it but unfortunately it doesnt.
Give a real word example. Regexmatches with word bounderies sounds like a reasonable solution.
Couldn't simplify it per your instructions
Code: Select all
SelectItemsInGroups("b", 3, 2, 10); // b = both, f = files, d = directories
function SelectItemsInGroups($types, $selSize, $skipSize, $limit=1) {
$matches = "";
$curSelSize = 0;
$curSkipSize = 0;
$curCount = 0;
foreach($item, listpane()) {
if ($curCount == $limit) { break; }
$type = exists($item);
if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
if ($curSelSize == $selSize) {
if ($curSkipSize == $skipSize) {
$curCount++;
$curSelSize = 1;
$curSkipSize = 0;
$matches = $matches . $item . "<crlf>";
} else { $curSkipSize++; }
} else {
$curCount++;
$curSelSize++;
$matches = $matches . $item . "<crlf>";
}
}
selectitems replace($matches, "<crlf>", "|");
}
Re: User Functions Exchange
Posted: 16 May 2016 22:11
by tiago
Thank you for the code.
Pseudo:
Code: Select all
$blacklist = "que,de";
$source = "um,a,para,que,quando,de,";
foreach($token, "$source", ",") {
$check = strpos("$blacklist", "$token");
if ($check != "-1") { continue; }
}
Re: User Functions Exchange
Posted: 16 May 2016 22:19
by highend
?
What has allstrpos to do with a blacklist?
I don't want pseudo code. Describe in plain, easy words what you actually want to do, the input AND the expected output!
Re: User Functions Exchange
Posted: 16 May 2016 22:29
by tiago
Well.
It's pseudo because strpos nor any known function I'm aware of can do it. strpos being the closer I get to this.
Per the request I'd like to have the ability to use words in the haystack which strpos as it is won't do.