mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2010
Posts: 969
F
Hoopy frood
OP Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
Using $pos(text, string, N)

Currently, if N <= 0, $pos returns the number of occurrences of string in text. With other string identifiers such as $left(), $mid(), $right(), $*tok, etc a negetive N causes the identifier to work in reverse.

It would be handy if $pos() would support such reversed behavior aswell

Code
Example:
  $pos(abccba, a, -1) - returns 6


I am SReject
My Stuff
Joined: Jul 2006
Posts: 4,149
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,149
Probably has been suggested many times in the past, still a good idea.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2012
Posts: 301
Pan-dimensional mouse
Offline
Pan-dimensional mouse
Joined: Jan 2012
Posts: 301
As a solution I created a self-written identifier for you: $posmir (position mirror) ‒ returns the position of found characters in the string in reverse order.

Syntax: $posmir(text,string,N) ‒ If it is convenient for you to specify negative values (N = -1) in your script, then it will work in the same way (analogically) as with numbers greater than zero.

Example1: $posmir(abccba abca cbcba,bc,0) ‒ Returns: 3
Example2: $posmir(abccba abca cbcba,bc,1) ‒ Returns: 14
Example3: $posmir(abccba abca cbcba,bc,3) ‒ Returns: 2
Example4: $posmir(abccbaabcacbcba,$chr(32),0) ‒ Returns: 2
Example5: $posmir(abccba abcacbcba,$chr(32),1) ‒ Returns: 12

Code
alias posmir {
  if (($1 != $null) && ($2 != $null) && ($3 != $null)) {
    if ($3 == 0) return $pos($1,$2,$3)
    var %pm1 $1 | var %pm2 $2 | var %pm3 $remove($3,-)
    var %pm_a $pos(%pm1,%pm2,0)
    var %pm_c $calc(%pm_a +1 - %pm3)
    var %pm_p $pos(%pm1,%pm2,%pm_c)
    if (%pm3 <= %pm_a) return %pm_p | else return 0
  }
  echo -sc info * insufficient parameters ‒ $+(correct syntax:,$color(notice).dd) $eval($posmir(text,string,N),0) | halt
}

Testing commands:
Code
//echo -a $posmir(abccba abca cbcba,bc,0)
//echo -a $posmir(abccba abca cbcba,bc,1)
//echo -a $posmir(abccba abca cbcba,bc,3)
//echo -a $posmir(abccba abca cbcba,$chr(32),0)
//echo -a $posmir(abccba abca cbcba,$chr(32),1)


This is probably what you would like to see. Though I also support your idea for realization negative values in built-in identifier "$pos".


Last edited by Epic; 28/05/21 04:33 PM. Reason: changes in code after comment from maroon

🌐 http://forum.epicnet.ru 📜 irc.epicnet.ru 6667 #Code | mIRC scripts, help, discuss, examples
Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
Just a minor point. Identifier calls should not return a string containing the error message. It should instead be echoed and the script halted.

return $+(,$color(info).dd,*) insufficient parameters - $+(correct syntax:,$color(notice).dd) $eval($posmir(text,string,N),0)

should become something like:

Code
echo -sc info insufficient parameters - $+(correct syntax:,$color(notice).dd) $eval($posmir(text,string,N),0)
halt


Unfortunately, an error message could only report a script/scriptline for the location of your alias. Without something like the previously requested $calias, it's not possible to report the scriptline/scriptname of the calling alias unless they're passed along as the $4 and $5 parms.

Joined: Apr 2010
Posts: 969
F
Hoopy frood
OP Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
I currently use the following:

Code
alias posx {
  if ($3 >= 0 || $calc($3+1+ $count($1,$2)) > 0) {
    return $pos($1,$2,$v1)
  }
}



Edited: Tinkered with the alias a bit to make it shorter

Last edited by FroggieDaFrog; 28/05/21 04:07 PM.

I am SReject
My Stuff
Joined: Jan 2012
Posts: 301
Pan-dimensional mouse
Offline
Pan-dimensional mouse
Joined: Jan 2012
Posts: 301
You're right. The identifier doesn't really need to return an error if multiple parameters are missing on run. It is enough to notify about this in an echo message and halted the script.

Therefore this line should be replaced:
Code
return $+(,$color(info).dd,*) insufficient parameters ‒ $+(correct syntax:,$color(notice).dd) $eval($posmir(text,string,N),0)

Replace with this line:
Code
echo -sc info * insufficient parameters ‒ $+(correct syntax:,$color(notice).dd) $eval($posmir(text,string,N),0) | halt

Thank you for your help with the hint cool


🌐 http://forum.epicnet.ru 📜 irc.epicnet.ru 6667 #Code | mIRC scripts, help, discuss, examples

Link Copied to Clipboard