develooper Front page | perl.beginners | Postings from January 2004

Re: Convert letter to phone keypad equivalent

Thread Previous
From:
James Edward Gray II
Date:
January 2, 2004 08:52
Subject:
Re: Convert letter to phone keypad equivalent
Message ID:
0AF696B2-3D44-11D8-8611-000A95BA45F8@grayproductions.net
On Jan 2, 2004, at 10:26 AM, Kevin Old wrote:

> Hello everyone,
>
> Here's a script I wrote to convert letters into their telephone keypad
> equivalents....example....A-B to 2, D-F to 3, etc.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $cnum = 'SM1550';
>
> $cnum =~ s/[A-C]/2/g;
> $cnum =~ s/[D-F]/3/g;
> $cnum =~ s/[G-I]/4/g;
> $cnum =~ s/[J-L]/5/g;
> $cnum =~ s/[M-O]/6/g;
> $cnum =~ s/[P-S]/7/g;
> $cnum =~ s/[T-V]/8/g;
> $cnum =~ s/[W-Y]/9/g;
>
> print "$cnum\n"
>
> This works like a charm, but I wondered if anyone knew of a way to
> consolidate this into fewer regex's.  Like my post earlier this week,
> I'm not trying to be lazy, just trying to learn other ways of
> programming/consolidating regexes.

See if this one liner puts you on the right track:

perl -e '$line = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $line =~ 
tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233333355566677778889999/; print 
"$line\n"'

James


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About