OKSD
asked on
Converting letters to numbers
This is probably quite simple, but say I wanted to convert A-Z to 10-36.
Can I do this any way other than $string =~ s/A/10/i; , etc. ?
Thanks,
Nik
Can I do this any way other than $string =~ s/A/10/i; , etc. ?
Thanks,
Nik
$string=~s/([A-Z])/10+ord( $1)-ord('A ')/eg;
ASKER
Ok, I'll try that, thanks. Could you explain a bit of that? I understand some of it, but what is +ord and -ord? and what is the 'e' appended at the end?
ASKER
And how would you effect the opposite? i.e., numerical to text.
Thanks
Thanks
perldoc -f ord
ord EXPR
ord Returns the numeric (the native 8-bit encoding,
like ASCII or EBCDIC, or Unicode) value of the
first character of EXPR. If EXPR is omitted, uses
$_.
For the reverse, see "chr". See perlunicode and
encoding for more about Unicode.
perldoc perlop
s/PATTERN/REPLACEMENT/egim osx
Options are:
e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
ord EXPR
ord Returns the numeric (the native 8-bit encoding,
like ASCII or EBCDIC, or Unicode) value of the
first character of EXPR. If EXPR is omitted, uses
$_.
For the reverse, see "chr". See perlunicode and
encoding for more about Unicode.
perldoc perlop
s/PATTERN/REPLACEMENT/egim
Options are:
e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
s/(\d\d)/chr($1-10+ord('A' ))/eg;
ASKER
Could you print an example of the reverse please?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, works perfectly. Thank you very much! ;)
-Nik
-Nik