Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: if,if and again,if

by moritz (Cardinal)
on Aug 21, 2012 at 09:54 UTC ( #988676=note: print w/ replies, xml ) Need Help??


in reply to if,if and again,if

You can try a data-driven approach:

my @matchers = ( qr/(Lz0|PLATO)/i, 'Apps', qr/(SKIDROW|POSTMORTEM|RELOADED|THETA|TiNYiSO|HI2U|TE|FLT|PROPHET| +Unleashed|OUTLAWS|ALiAS|JAGUAR)/i, 'Games', ... ); for (my $idx = 0; $i < @matchers; $i += 2) { if ($release_name =~ $matchers[$i]) } $category = $matchers[$i + 1]; print "[INFO] Category: $category"; last; # stop after the first category was found } } # code not tested


Comment on Re: if,if and again,if
Download Code
Re^2: if,if and again,if
by cesapun (Acolyte) on Aug 21, 2012 at 10:54 UTC

    Many thanks.That works.

    Close this.
Re^2: if,if and again,if
by Tanktalus (Canon) on Aug 23, 2012 at 17:57 UTC

    Data-driven approaches FTW!

    Though I'd be tempted to do things a bit more perlishly (at least for my own definition of perlish):

    my @matchers = ( [ qr/(Lz0|PLATO)/i => 'Apps' ], ... ); for my $check (@matchers) { if ($release_name =~ $check->[0]) { $category = $check->[1]; print "[INFO] Category: $category\n"; last; } }
    Actually, a bit more perlish, IMO, would be to replace that for loop altogether:
    use List::Util qw(first); my $category = map { $_ ? $_->[1] : undef; } first { $release_name =~ $_->[0]; } @matchers; if ($category) { print "[INFO] Category: $category\n"; } else { print "[ERROR] Unrecognised release type: $release_name\n"; }
    But maybe that's just me. :-)

      If we go all perlish, I'd get rid of the ugly map with a ternary :-). Either

      my $category = first { ... } ... ; if ($category) { $category = $category->[1]; print "[INFO] Category: $category\n"; }

      Or if you insist on a map on a one-element list, write it as

      map $_->[1], grep $_, first { .... } ...;

      IMHO several simple operations are easier to read than a few complicated ones.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-09-23 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (221 votes), past polls