Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found