Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

nested if else

by sarvan (Sexton)
on Jul 13, 2011 at 05:17 UTC ( [id://914057]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there,

I have the following code with nested if else. Two if conditions are nested. In the first if i check on condition. if it satisfies then go into the 2nd if. In the 2nd if i route the prgm to different other programs based on the condition satisfied.

But, now it takes me directly to the else of 1st if without executing the 2nd if and its condition's. I have no idea where i did mistake. Can anyone help me.

#!/usr/bin/perl use strict; use warnings; print "Which Metric jaccard or Bleu:"; my $metric=<>; print "Which source title or snippet or url:"; my $source=<>; if($metric eq "jaccard") { if($source eq "title") { print "in title"; system("/usr/bin/perl filecom.pl") } elsif($source eq "snippet") { print "in snippet"; system("/usr/bin/perl searchsnipdup.pl") } elsif($source eq "url") { print "in url"; print"jaccard method not called"; } else { print "jacccard not called"; } } elsif($metric eq "bleu") { print "bleu called"; } else { print "bleu not called"; }

Replies are listed 'Best First'.
Re: nested if else
by kcott (Archbishop) on Jul 13, 2011 at 06:07 UTC

    Your problem is that you're doing an exact match (with eq) against the alphabetic characters but not the terminal newline. For example, if I type in jaccard, then $metric will be the string "jaccard\n". You should probably chomp the input prior to processing it, e.g.

    my $metric = <>; chomp $metric;

    Example of your code as posted:

    Which Metric jaccard or Bleu:jaccard Which source title or snippet or url:title bleu not called

    Example after chomping $metric and $source:

    Which Metric jaccard or Bleu:jaccard Which source title or snippet or url:title in title

    P.S. I also notice you prompt for "Bleu" but test for "bleu": these strings are not equal.

    -- Ken

Re: nested if else
by lidden (Curate) on Jul 13, 2011 at 06:04 UTC
    Your variables will contain newline chars. All your ifs will fail. Try chomp $metric; after you have read something into it.
Re: nested if else
by Marshall (Canon) on Jul 13, 2011 at 13:47 UTC
    Appropriate use of white space is one of the most important things that you can do to improve program legibility.
    use strict; use warnings; print "Which Metric jaccard or Bleu:"; my $metric=<>; chomp $metric; print "Which source title or snippet or url:"; my $source=<>; chomp $source; if($metric eq "jaccard") { if($source eq "title") { print "in title"; system("/usr/bin/perl filecom.pl") } elsif($source eq "snippet") { print "in snippet"; system("/usr/bin/perl searchsnipdup.pl") } elsif($source eq "url") { print "in url"; print"jaccard method not called"; } else { print "jacccard not called"; } } elsif($metric eq "bleu") { print "bleu called"; } else { print "bleu not called"; }
Re: nested if else
by BrowserUk (Patriarch) on Jul 13, 2011 at 05:24 UTC

    Have you heard the word "indentation"?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-16 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found