This line is wrong:

if ($number4 == 1,2) {

I think you probably expected that $number4 will compare to 1 or to 2, and that if it is equal to either 1 or 2 you will have a Boolean "true" condition. But what really happens is that $number == 1 is evaluated, and then the result is thrown away. Then 2 is evaluated, and its value in a Boolean context turns out to be "true". So your conditional will always be true.

Consider this example:

print "Yes!\n" if ( 1 == 2, 3 );

The outcome will be "Yes!", because even though the conditional 1==2 is false, that evaluation is thrown away, and the "3" is evaluated numerically in Boolean context, and that will always be "true".

You probably want to say something like this:

if( grep { $number4 == $_ } 1, 2 ) {...

Or more simply...

if( $number4 == 1 || $number4 == 2 ) { ...

Or more legibly:

use List::Util 'any'; # ......... if( any { $number4 == $_ } 1, 2 ) {...

Dave


In reply to Re: Perl compare by davido
in thread Perl compare by waytoperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.