Solved

Converting letters to numbers

Posted on 2003-10-30
8
419 Views
Last Modified: 2010-03-04
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
0
Comment
Question by:OKSD
  • 4
  • 4
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 9654142
$string=~s/([A-Z])/10+ord($1)-ord('A')/eg;
0
 
LVL 1

Author Comment

by:OKSD
ID: 9654215
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?
0
 
LVL 1

Author Comment

by:OKSD
ID: 9654235
And how would you effect the opposite? i.e., numerical to text.

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 9654253
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/egimosx
 
           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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
ID: 9654300
s/(\d\d)/chr($1-10+ord('A'))/eg;
0
 
LVL 1

Author Comment

by:OKSD
ID: 9654311
Could you print an example of the reverse please?

Thanks
0
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 9654356
$_ = join' ','A'..'Z',"\n";
s/([A-Z])/ord($1)-ord('A')+10/eg;
print;
s/(\d\d)/chr($1+ord('A')-10)/eg;
print;
0
 
LVL 1

Author Comment

by:OKSD
ID: 9654363
Ok, works perfectly. Thank you very much! ;)

-Nik
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Tutorial 6 127
iSeries PERL Scripts 7 132
Linux rename files by reordering dates in the file name 6 31
Perl File::Find alternative 1 28
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now