Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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]
[shmem]: moin Corion, karlgoethebier. I stumbled over <<>> occasionally on the p5p mailing list
[Discipulus]: not noticed
[karlgoethebier]: sometimes rereading manages is helpful ;-) at least for me...
[Eily]: hello people \o/
[karlgoethebier]: manpages..
[Eily]: and non-people and NodeReaper

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-07-21 07:59 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.