mIRC Home    About    Download    Register    News    Help

Print Thread
$pos() - Support negative indexes #269007 27/05/21 04:46 AM
Joined: Apr 2010
Posts: 966
F
FroggieDaFrog Offline OP
Hoopy frood
OP Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 966
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
Re: $pos() - Support negative indexes [Re: FroggieDaFrog] #269013 27/05/21 03:02 PM
Joined: Jul 2006
Posts: 3,790
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,790
Probably has been suggested many times in the past, still a good idea.


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: $pos() - Support negative indexes [Re: FroggieDaFrog] #269025 28/05/21 03:04 PM
Joined: Jan 2012
Posts: 151
Epic Offline
Vogon poet
Offline
Vogon poet
Joined: Jan 2012
Posts: 151
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

🅸🆁🅲 - 𝔦'𝔱𝔰 𝔸 𝕂𝖎ɴ𝙙 ᴏ𝙛 𝕄𝙖𝖌𝙞𝙘
Re: $pos() - Support negative indexes [Re: Epic] #269026 28/05/21 03:55 PM
Joined: Jan 2004
Posts: 1,590
maroon Offline
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 1,590
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.

Re: $pos() - Support negative indexes [Re: Epic] #269027 28/05/21 04:02 PM
Joined: Apr 2010
Posts: 966
F
FroggieDaFrog Offline OP
Hoopy frood
OP Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 966
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
Re: $pos() - Support negative indexes [Re: maroon] #269028 28/05/21 04:17 PM
Joined: Jan 2012
Posts: 151
Epic Offline
Vogon poet
Offline
Vogon poet
Joined: Jan 2012
Posts: 151
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


🅸🆁🅲 - 𝔦'𝔱𝔰 𝔸 𝕂𝖎ɴ𝙙 ᴏ𝙛 𝕄𝙖𝖌𝙞𝙘