Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 nobody stirs...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2017-09-22 16:14 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (264 votes). Check out past polls.