mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Feb 2005
Posts: 9
K
Kaepez Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
K
Joined: Feb 2005
Posts: 9
Back again, I suppose.

Just a quick question about the capabilites of mirc (I hope this is in the right place).

Is there a function to turn a letter into a number (a = 1, b = 2, etc.) and vice versa?

If not, what would be the best way to go about doing so (the only thing that comes to mind is manually assigning a value to return for each letter/number)?

Thanks.

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
Useage: $alnumcon(<L> or <N>) where L is any letter in the range a-z and N is an number in the range 1-26.

Examples:
//echo -a $alnumcon(26) ---> z
//echo -a $alnumcon(e) ---> 5
//echo -a $alnumcon(abc) ---> Invalid Parameter

Code:
alias alnumcon {
  var %a = abcdefghijklmnopqrstuvwxyz, %c
  if (($1 isin %a) &amp;&amp; $len($1) == 1) %c = $pos(%a,$1,1)
  else if ($1 isnum 1-26) %c = $mid(%a,$1,1)
  else %c = $!alnumcon - Invalid Parameter
  return %c
}

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Code:
alias alpha2num {
  if ($1 isnum 1-26) { return $chr($calc(96 + $1)) }
  if ($left($1,1) isalpha) { return $calc($asc($lower($v1)) - 96) }
}


//echo -a $alpha2num(a) $alpha2num(z) $alpha2num(A) $alpha2num(Z) $alpha2num(1) $alpha2num(26)
1 26 1 26 a z

any out of bounds value results in $null returned, multiple characters passed well result in the first character being evaluated ie: $alpha2num(ABC) => 1

Yell out if u want one to do multiple letters returning a string of numbers etc.

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
I thought about your multiple letters/numbers, and being that I was extremely bored, I came up with the following (also using your conversion method, since it works best for this):

Code:
alias alpha2num {
  var %a = $1-, %r = /(?:[a-z]{2,}|[^a-z])/giS, %c
  !.echo -q $regsub(%a,%r,,%c) $regsub(%c,/([a-z])/giS,$chr(32) \$calc(\$asc(\$lower(\1)) - 96),%c)
  return $(%c,2)
}

alias num2alpha {
  var %n = $1-, %r /(?:\D{1,}|-\d{1,}|\d{3,}|0|2[7-9])\b/g, %c
  !.echo -q $regsub(%n,%r,$chr(32),%c) $regsub(%c,/\b([1-9]|1[0-9]|2[0-6])\b/gS,$chr(32) \$chr(\$calc(\1 + 96)),%c)
  return $(%c,2)
}


Useage:

$alpha2num(a b x y z) ---> 1 2 24 25 26
$alpha2num(u d j 1111 xp u @) ---> 21 4 10 21

$num2alpha(2 1 9 5) ---> b a i e
$num2alpha(1 26 0 25 24 -10 23 2 200 3 4) ---> a z y x w b c d

Both will ignore invalid input as shown in the results.

BTW, this also works for single inputs (e.g. "a" or "22").

And here comes FiberOPtics to point out my mistakes. I have a good excuse this time though, it's 6:00 AM and I'm lacking sleep. :tongue:

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
The $regsub() and $(,2) trick is severely limited in the length of input it can handle (in your case it's 32 characters). $regsubex() was added exactly for what you want to accomplish: \1 can be passed directly to identifiers that are inside the 3rd parameter. The only catch is that $regsubex() is only available in v6.17.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
It's actually 33 for alpha2num and 45-47 (inputs) for num2alpha depending the numbers you use. I'm aware of the limitation, however I couldn't get it to work with regsubex. In my attempt, it would keep evaluating the same character (e.g. 'a b c' would evaluate a 3 times). Maybe I'll give it another try.

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
heres an alternative, codes not so compact, but it does more chaeracters

Code:
alias alpha2num {
  bset -t &amp;binvar 1 $lower($1)
  tokenize 32 $bvar(&amp;binvar,1,999)
  bunset &amp;binvar
  bset &amp;binvar 1 255
  alpha2num.call $*
  return $bvar(&amp;binvar,2,999)
  :error | reseterror
}
alias -l alpha2num.call { if ($1 isnum 97-122) bset &amp;binvar $calc($bvar(&amp;binvar,0) + 1) $calc($1 - 96) }

alias num2alpha {
  bset &amp;binvar 1 255
  tokenize 32 $1
  num2alpha.call $*
  return $bvar(&amp;binvar,2,999).text
  :error | reseterror
}
alias -l num2alpha.call { if ($1 isnum 1-26) bset &amp;binvar $calc($bvar(&amp;binvar,0) + 1) $calc($1 + 96) }


Link Copied to Clipboard