Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Trying to compare a string...

by moritz (Cardinal)
on Oct 18, 2013 at 08:26 UTC ( #1058723=note: print w/replies, xml ) Need Help??

in reply to Trying to compare a string...

In addition to choroba's excellent answer, I'd like to point out that you can replace code with data like this:

my %genre_map = ( 'Science Fiction' => 'Sci-Fi & Fantasy', 'Sci-FI' => 'Sci-Fi & Fantasy', 'Fantasy' => 'Sci-Fi & Fantasy', 'Action' => 'Action & Adventure', ... ); my $genre = $genre_map{$genres[0]} // $genres[0];

Factoring the code like this would even allow you to put the genre mapping into a configuration file, and load the hash from it.

Finally, Perl 6 has a feature called junctions which does allow you to write code similar to what you tried:

use v6; my $genre; my @genres = 'Kids', 'Ignored'; if @genres[0] eq 'Science Fiction' | 'Sci-Fi' | 'Fantasy' { $genre = 'Sci-Fi & Fantasy'; } elsif @genres[0] eq 'Action' | 'Adventure' | 'War' { $genre = 'Action & Adventure'; } elsif @genres[0] eq 'Kids' | 'Family' { $genre = 'Kids & Family'; } else { $genre = @genres[0]; } say $genre;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058723]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2018-01-23 00:10 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.