script to select files, folders in groups
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
script to select files, folders in groups
[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
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
Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
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...
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;
}
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
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.
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.
Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
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...
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...
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
wait...
is this wrong?
Initial test passed ok.
What's your opinion?
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;
}
What's your opinion?
Power-hungry user!!!
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
highend wrote:Why should a script e.g. process 1000 items when your limit is e.g. 50.
Yes, I see your point now...
Last edited by tiago on 30 Apr 2016 20:04, edited 1 time in total.
Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
Would work but as I already said, it's bad when you have a low limit and a large number of items...
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
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
}Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
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.
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
I'm breaking the script with attempts of single check.
Power-hungry user!!!
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
Hiya, SammaySarkar!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.
...
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...
Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
Give a real word example. Regexmatches with word bounderies sounds like a reasonable solution.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.
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>", "|");
}
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
Thank you for the code.
Pseudo:
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; }
}
Power-hungry user!!!
-
highend
- Posts: 14940
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: User Functions Exchange
?
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!
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!
One of my scripts helped you out? Please donate via Paypal
-
tiago
- Posts: 589
- Joined: 14 Feb 2011 21:41
Re: User Functions Exchange
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.
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.
Power-hungry user!!!
XYplorer Beta Club