Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Output Repeats in the elsif statement

by PilotinControl (Scribe)
on Feb 13, 2013 at 20:40 UTC ( #1018630=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

The elsif statement repeats the output and I am not sure why

sub viewrailcarsroadname{ no warnings 'redefine'; no warnings 'uninitialized'; print color 'green'; open(MYINPUTFILE, "railcardata.txt"); # open for input $| = 1; my @lines = <MYINPUTFILE>; # read file into list close (MYINPUTFILE); printf (" ===============================\n\n" +); printf (" | VERIFY CAR ROAD NAME |\n\n" +); printf (" ===============================\n\n" +); printf (" \n\n" +); printf (" PLEASE INSERT ROADNAME TO VERIFY:\n\n +"); printf (" \n\n\n\n\n\n\n\n"); my $carroadnameverify = <STDIN>; chomp($carroadnameverify); printf ("\n ================================================== +=================\n\n"); printf (" | ROAD NAME | ROAD NUMBER | CAR TYPE | INTERCH +ANGE RAILROAD |\n\n"); printf (" ==================================================== +===============\n\n\n"); foreach my $carroadnameverify2 (@lines) { if ($carroadnameverify2 =~ /$carroadnameverify/) { my $format = " %-13s %-15s %-12s %0s"; printf ("$format", split/\:/, "$carroadnameverify2"); } elsif ($carroadnameverify2 =~ '') { printf ("CAR RECORD NOT FOUND"); } } # END FILE LOOP printf ("\n\n\n\n WHEN YOU ARE DONE VIEWING HIT RE +TURN TWICE:\n\n\n\n\n\n\n\n"); my $input = <STDIN>; chomp($input=<STDIN>); structure::railcar(); } # END VIEW SUB

Comment on Output Repeats in the elsif statement
Download Code
Re: Output Repeats in the elsif statement
by Kenosis (Priest) on Feb 13, 2013 at 21:08 UTC

    Were you trying to test whether $carroadnameverify2 is null in the elsif statement? If so, I think you meant this:

    ... elsif ($carroadnameverify2 eq '') { ...

    $carroadnameverify2 =~ '' will evaluate to true, regardless of $carroadnameverify2's value or whether it's even defined.

      changing it to "eq" causes the output to be blank not returning the Car Record Not Found...leaving it at =~ causes the Car Record Not Found to be repeated over and over for three lines of output...thats the problem

        Ah! Just use else:

        else { printf("CAR RECORD NOT FOUND"); }

        As your if tests for the record...

Re: Output Repeats in the elsif statement
by jwkrahn (Monsignor) on Feb 13, 2013 at 21:56 UTC
    if ($carroadnameverify2 =~ /$carroadnameverify/) { ... } elsif ($carroadnameverify2 =~ '') {

    From perlop:

    The empty pattern //
    If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead. In this case, only the "g" and "c" flags on the empty pattern is honoured - the other flags are taken from the original pattern. If no match has previously succeeded, this will (silently) act instead as a genuine empty pattern (which will always match).

Re: Output Repeats in the elsif statement
by nvivek (Vicar) on Feb 14, 2013 at 05:23 UTC

    If you want to check $carroadnameverify2 as empty, you can try following.

    elsif(!defined $carroadnameverify2 && $carroadnameverify2 =~ /^\s*$/)

    Above condition will print CAR RECORD NOT FOUND message when $carroadnameverify2 is undef or null or string with blank characters.

      Thank you perl monks! The solutions given worked! Much appreciated. Thanks again!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-11-28 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls