Hi all,

I'm trying to see which is easier for maint. I have the following and wondering if I should replace it with map and grep. Not sure why, but find myself not reaching for them ever. This turns letters on your phone dialpad to numbers:

use strict; use warnings; use 5.10.1; my $filter_clean = 'GAV18'; my %filter_map = ( A => 2, B => 2, C => 2, D => 3, E => 3, F => 3, G => 4, H => 4, I => 4, J => 5, K => 5, L => 5, M => 6, N => 6, O => 6, P => 7, Q => 7, R => 7, S => 7, T => 8, U => 8, V => 8, W => 9, X => 9, Y => 9, Z => 9, ); # 1st version my $filter = q{}; if ($filter_clean) { my @filter_split = split //, $filter_clean; for my $fc (@filter_split) { if ( $fc =~ /\d/ ) { $filter .= $fc; } else { $filter .= $filter_map{ uc $fc }; } } } say $filter; # 2nd version. Why would I use below (when it's working right, that is +) over above? $filter .= map { $filter_map{ uc $_ } } grep { !/\d/ and $filter_clean } split//, $filter_clean; say $filter; [ghenry@linux]$ perl t/map.pl 42818 428183

I also need to make this work properly so I'm not returning the amount of matches grep has.

Advice? I prefer my first version for when I come back later.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

In reply to map and grep or clear code? by ghenry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.