in reply to using if-elsif-else
Hello thevoid,
You're fairly close - you just have the syntax a bit off.
The general structure is this:
if ( condition ){ # code } elsif ( condition ){ # code } else { # default case, in case no conditions match }
So your code should look like this:
#!/usr/bin/perl #hash.plx use warnings; use strict; my %phnums = ( derrick => '07786255633', juan => '07798685311', kevin => '07740647707', ); print "Enter a name : "; my $name = <STDIN>; chomp ($name); if ($name =~ /^derrick\b/i) { print $phnums{"derrick"}; } elsif ($name =~ /^juan\b/i) { print $phnums{"juan"}; } else { print $phnums{"kevin"}; }
Note, if you enter "fred", you'll see Kevin's number. So you might instead want:
#!/usr/bin/perl #hash.plx use warnings; use strict; my %phnums = ( derrick => '07786255633', juan => '07798685311', kevin => '07740647707', ); print "Enter a name : "; my $name = <STDIN>; chomp ($name); if ($name =~ /^derrick\b/i) { print $phnums{"derrick"}; } elsif ($name =~ /^juan\b/i) { print $phnums{"juan"}; } elsif ($name =~ /^kevin\b/i) { print $phnums{"kevin"}; } else { print "Name not found."; }
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
|
|---|