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

Hello all you Perl Guru's!!!! Hey I'm stuck, can you help me please??

Vars in file1: $dlci, $router, and $cir and Vars in file2: $spdlci, $sprouter, and $spcir

I am trying to:
  if  ($dlci eq $spdlci) and ($router eq $sprouter){
but
if ($dlci eq $spdlci) && ($router eq $sprouter){
and
if ($dlci eq $spdlci && $router eq $sprouter){
won't work.... can you share your wisdom,,,,please? Thank You!!

Replies are listed 'Best First'.
Re: if and
by BeernuT (Pilgrim) on Feb 20, 2002 at 04:08 UTC
    if(($dlci eq $spdlci) and ($router eq $sprouter)) {
    enclose them in ()'s

    -bn
      Thank you BeernuT!!!! It works!!!!
Re: if and
by vek (Prior) on Feb 20, 2002 at 05:08 UTC
    Hmm, your example code works fine for me:
    #!/usr/bin/perl -w use strict; my $foo1 = "kev"; my $bar1 = "kev"; my $foo2 = "spencer"; my $bar2 = "spencer"; if ($foo1 eq $bar1 && $foo2 eq $bar2) { print "yep, works fine for me\n"; }
    Yields the following:
    %foo.pl yep, works fine for me %
      While this code example does still work, it represents, to a certain extent, a failure in development logic.

      The case scenario given is that two conditions must be true for a true result to the expression - The order of developer logic should follow testing each condition individually and then applying the AND logic to the condition results. The application of conditions within parentheses as given by BeernuT follows this order.

      With the code given, this only works because of the precedence of operands as described in perlop.

       

      Update

      By failure of development logic, I refer not to the deparsing of code, because as highlighted by blakem below (++blakem), the result is identical, but rather the logic driving the writing of the code. In this instance, Anonymous Monk voiced confusion in order and syntax, presumably the result of trying to merge everything together without respect to the individual order of elements within the logic process.

      This node was intended to direct discussion towards cause moreso than effect.

       

      perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

        Assuming you're discussing (($x1 eq $y1) and ($x2 eq $y2)) vs. ($x1 eq $y1 && $x2 eq $y2)

        Since, they deparse to the same thing, how can one be a "failure"? Are you claiming that they do different things, or that one is harder to understand and maintain?

        % perl -MO=Deparse -e'if($x1 eq $y1 && $x2 eq $y2){}' if ($x1 eq $y1 and $x2 eq $y2) { (); } % perl -MO=Deparse -e'if(($x1 eq $y1) and ($x2 eq $y2)){}' if ($x1 eq $y1 and $x2 eq $y2) { (); }

        -Blake