Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: map and grep or clear code?

by tobyink (Canon)
on Jan 31, 2012 at 11:04 UTC ( [id://950950]=note: print w/replies, xml ) Need Help??


in reply to map and grep or clear code?

Personally I'd find a simple regex s/// simpler than all of the above:

use strict; use warnings; use 5.10.1; my $filter_clean = 'GAV18'; my %filter_map = ( A => 2, B => 2, # ... Y => 9, Z => 9, ); $filter_map{$_} = $_ for 0..9; (my $output = uc $filter_clean) =~ s{ (.) }{ $filter_map{$1} }exg; say $output;

Replies are listed 'Best First'.
Re^2: map and grep or clear code?
by brx (Pilgrim) on Jan 31, 2012 at 18:59 UTC
    No need to extend the hash (with 0..9) :
    (my $output = $filter_clean) =~ s{ (\D) }{ $filter_map{uc $1} }exg;
    If the aim isn't to separate data and function :
    use strict; use warnings; use 5.10.1; my $filter_clean = 'GaV18'; sub filter2num { local $_=shift; s/[ABC] /2/igx; s/[DEF] /3/igx; s/[GHI] /4/igx; s/[JKL] /5/igx; s/[MNO] /6/igx; s/[PQRS]/7/igx; s/[TUV] /8/igx; s/[WXYZ]/9/igx; return $_; } say filter2num($filter_clean);

      Even something like:

      $filter_clean =~ tr/A-Z/22233344455566677778889999/;

      ought to do.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://950950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found