Scripting: goes OOP? Provide auto count of functions

Features wanted...
Post Reply
Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Scripting: goes OOP? Provide auto count of functions

Post by Stefan »

Question to Don:

I am not sure if i can make myself clear and i don't know if you have this info, but...

... I guess for commands like
listfolder()
replace()
inputselect()
you already could provide an count to us? Or at least you would be able to do so?


So we can let be our script KISS if you would auto generate an var
with name of the original var but added an "Count"
where you give us the amount of iteration you had done in the back ground.

Examples:

$ITEMS = listfolder();
msg "Items in folder: " . $ITEMS_count;

$ITEMS = replace();
msg "Needle found: " . $ITEMS_count . " times";

where $ITEMS_count is an auto generated var based on the user var $ITEMS


The var name is of course only an example an could be something like _XYAutoCount to not compromise existent scripts.


What do you think?

admin
Site Admin
Posts: 66366
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: goes OOP? Provide auto count of functions

Post by admin »

I'd rather give you a way to count the tokens in a list, CountToken() or so.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Scripting: goes OOP? Provide auto count of functions

Post by Stefan »

admin wrote:I'd rather give you a way to count the tokens in a list, CountToken() or so.
$Amount = CountToken($Items, "|") -1; ?

Sounds fine too to me. Even better. Thanks.

maybe
gettoken(string, [index=1 | "count"], [delimiter=" "])
$Amount = gettoken($Items, "Count", "|");

admin
Site Admin
Posts: 66366
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: goes OOP? Provide auto count of functions

Post by admin »

Stefan wrote:gettoken(string, [index=1 | "count"], [delimiter=" "])
$Amount = gettoken($Items, "Count", "|");
Good idea, done! :)

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Scripting: count option for functions(), gettoken(count)

Post by Stefan »

Code: Select all

v9.30.0029 - 2010-08-01 17:32
    + SC GetToken enhanced. Now you can pass "count" as second parameter
      to retrieve the token count in the string. Note that the function
      returns 0 when the string is empty.
First test works very good. Thank you Don.

Code: Select all

//IF (Items empty)        {Count = 0}
//IF (delimiter not found){Count = 1}
//IF (delimiter found)    {Count = 2 && incr Count for each delim found after}

//TEST 1
$ITEMS = ""; //Count:0
  $ITEMS = "eins"; //Count:1
  $ITEMS = "eins|zwei 2"; //Count:2
  $ITEMS = "|"; //Count:2
  $ITEMS = "eins||"; //Count:3
  $ITEMS = "eins|zwei 2|drei| vier "; //Count:4
  $ITEMS = "eins|zwei 2|drei| vier |"; //Count:5
  msg gettoken($ITEMS, "count","|") - 1; //Count:4


//TEST 2
 $ITEMS = "|eins|zwei 2|| vier |";

  //Automatic clean up delimiters:
  $DELIMRegEx = "\|"; //Can be any char/sign but escape  "[\^$.|?*+()"-signs by an leading \ like "\|"
  $DELIM      = "|";
 
  $ITEMS = regexreplace($ITEMS,       $DELIMRegEx . "{2,}", $DELIM);  //remove two or more delimiters in a row
  $ITEMS = regexreplace($ITEMS, "^" . $DELIMRegEx);                   //remove leading delimiter
  $ITEMS = regexreplace($ITEMS,       $DELIMRegEx . "$");             //remove trailing delimiter
  $ITEMS = regexreplace($ITEMS, "^" . $DELIMRegEx . "$");             //remove solitary delimiter
  msg gettoken($ITEMS, "count", $DELIM); //Count:3

Post Reply