Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

if,if and again,if

by cesapun (Acolyte)
on Aug 21, 2012 at 09:14 UTC ( #988660=perlquestion: print w/ replies, xml ) Need Help??
cesapun has asked for the wisdom of the Perl Monks concerning the following question:

##################Setting category########################## if ($release_name =~ m/(Lz0|PLATO)/i) { print "[INFO] Category : Apps\n"; $category = "Apps"; #do smth } if ($release_name =~ m/(SKIDROW|POSTMORTEM|RELOADED|THETA|TiNYiSO| +HI2U|TE|FLT|PROPHET|Unleashed|OUTLAWS|ALiAS|JAGUAR)/i) { print "[INFO] Category : Games\n"; $category = "Games"; #do smth } if ($release_name =~ m/(DVDRip.XviD|dvdrip.xvid|TS|CAM|R5.XviD)/i) + { print "[INFO] Category : XviD\n"; $category = "XviD"; #do smth } if ($release_name =~ m/BluRay.x264/i) { print "[INFO] Category : HD x264\n"; $category = "HD x264"; #do smth } if ($release_name =~ m/DVDR/i) { print "[INFO] Category : XviD\n"; $category = "XviD"; #do smth } if ($release_name =~ m/(BRRIP.x264|BRRIP x264)/i) { print "[INFO] Category : XviD\n"; $category = "XviD"; #do smth } } if ($release_name =~ m/(BRRIP.XviD|BDRip.XviD)/i) { print "[INFO] Category : XviD\n"; $category = "XviD"; #do smth } if ($release_name =~ m/Update/i) { print "[INFO] Category : Misc\n"; $category = "MIsc"; #do smth } if ($release_name =~ m/(HDTV.XviD|HDTV.x264|PDTV.x264)/i) { print "[INFO] Category : Episodes\n"; $category = "Episodes"; #do smth }

Instead of all those IFs ,how can i make this part of code shorter ?

Also,if $release_name is(ie) "The.Avengers.2012.720p.BluRay.x264.DTS-HDChina" ,when i run the script,i get:

[INFO] Category : XviD [INFO] Category : HD x264
.

Matched xvid because of TS from DTS.I want it to match only INFO Category : HD x264

Many thanks in advance.

Comment on if,if and again,if
Select or Download Code
Re: if,if and again,if
by nemesdani (Friar) on Aug 21, 2012 at 09:29 UTC
    With or for example.
    A . has a special meaning, by the way. Look it up.
    And this isn't a tutorial, you posted in the wrong section.
    And it's not elegant letting everybody know that you're dowloading films illegally.

    I'm too lazy to be proud of being impatient.

      And it's not elegant letting everybody know that you're dowloading films illegally.

      assume much? :)

Re: if,if and again,if
by cesapun (Acolyte) on Aug 21, 2012 at 09:49 UTC
    I'm sorry for posting in the wrong section.Hope this topic can be moved.

    Also,i don't think that "or" can answer to my question

    Btw,i don't care if everybody knows that i'm a "pirate".Arhhh
      Us monks should not criticize what Perl is used for. Whther perl is for 1337 2p107z or ERP. The same way a mechanic shouldn't turn away a customer because the mechanic thinks the car was in a bank robbery because of stain on the pass seat.
Re: if,if and again,if
by moritz (Cardinal) on Aug 21, 2012 at 09:54 UTC

    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

      Many thanks.That works.

      Close this.

      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: perlquestion [id://988660]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-10-20 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (93 votes), past polls