Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

using if-elsif-else

by thevoid (Scribe)
on Dec 28, 2006 at 15:37 UTC ( [id://592065]=perlquestion: print w/replies, xml ) Need Help??

thevoid has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm back with another schoolboy error. I'm trying to write a wee program to access a hash value (names / phone no's) from the user inputting the key-
#!/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 { ($name =~ /^kevin\b/i) { print $phnums{"kevin"}; } } }
but I keep getting the error syntax error at -path- line 17, near "elsif }", or something similar depending on how I've tried to arrange the blocks and curly brackets. I've looked at several tutorials for examples and can't even find it in perldoc or perlfaq. Can someone please tell me where I'm going wrong?

Cheers, Paul.

Replies are listed 'Best First'.
Re: using if-elsif-else
by chargrill (Parson) on Dec 28, 2006 at 15:46 UTC

    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."; }


    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: using if-elsif-else
by monkey_boy (Priest) on Dec 28, 2006 at 15:46 UTC
    lots of extra brackets there!, also, an "else" cannot have a conditional, so you need another "elsif", use "else" for a catchall at the end, ive fixed up your snippet below:
    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 "No such person!\n"; };



    This is not a Signature...
Re: using if-elsif-else
by pajout (Curate) on Dec 28, 2006 at 16:05 UTC
    BTW, if the theme of the example code is hash value access, you would not use the if-elsif-else clause in that manner, which could be, imho, confusing for beginners - it is not using hash in the most typical way. I think then following pice of code is slightly better for demonstration:
    ... chomp ($name); if (exists $phnums{$name}) { print $phnums{$name}; } else { print "Name $name does not exists in the hash."; }

      I was operating under the assumption that a user could enter "Kevin Johnson" and still get the value for $phnums{'kevin'} - i.e. more than what might be used as the explicit hash key. Possibly a poor assumption, but I couldn't think of a better reason for using regexen in the OP's code :-)

      I recently ran across a hash setup as a dispatch table where most values were well defined, but some could match a variety - these were handled (in a generalized manner) like so:

      if( exists $hash{$key} ){ &$hash{$key}; } elsif( $key =~ /phrase/ ){ # several could match but all required the same action &$hash{'some_action'}; } else { &$hash{'default_action'}; }


      --chargrill
      s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
        I was operating under the assumption that a user could enter "Kevin Johnson" and still get the value for $phnums{'kevin'} - i.e. more than what might be used as the explicit hash key.

        You're right, that was the idea :-) Thanks for the alternative code.

      Ah, nice one. I hadn't really covered exists yet but had seen it mentioned, that makes sense.
Re: using if-elsif-else
by MonkE (Hermit) on Dec 28, 2006 at 15:58 UTC
    You are suffering from an excess of curly braces. Lose the curly brace that you have right after each elsif, and also the "matching" curly braces at the end of your else statement. The whole point of elsif is to reduce the clutter of curly braces.

    An if ...elsif ... elsif ... else statement looks like this:

    if (condition1) { code block } elsif (condition2) { code block } elsif (condition3) { code block } else { code block }
    Notice the location of the curly braces. Good luck.
Re: using if-elsif-else
by alpha (Scribe) on Dec 28, 2006 at 15:47 UTC
    chomp (my $name = <STDIN>); if ($name =~ /^derrick\b/i) { print $phnums{"derrick"}; } elsif ($name =~ /^juan\b/i) { print $phnums{"juan"}; } elsif ($name =~ /^kevin\b/i) { print $phnums{"kevin"}; }
    Also you sould Read The Frienly Manuals more often :) Update:
    chomp(my $name = <STDIN>); $name =~ /^$_/ and print ($phnums{$_}.chr 10) for keys %phnums
Re: using if-elsif-else
by thevoid (Scribe) on Dec 28, 2006 at 16:02 UTC
    excellent, thank you all! ;-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://592065]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found