mIRC Homepage
Posted By: FroggieDaFrog $pos() - Support negative indexes - 27/05/21 04:46 AM
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
Posted By: Wims Re: $pos() - Support negative indexes - 27/05/21 03:02 PM
Probably has been suggested many times in the past, still a good idea.
Posted By: Epic Re: $pos() - Support negative indexes - 28/05/21 03:04 PM
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".

Posted By: maroon Re: $pos() - Support negative indexes - 28/05/21 03:55 PM
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.
Posted By: FroggieDaFrog Re: $pos() - Support negative indexes - 28/05/21 04:02 PM
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
Posted By: Epic Re: $pos() - Support negative indexes - 28/05/21 04:17 PM
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
© mIRC Discussion Forums