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.