Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Multiple check in If statement ??

by PonGopi (Initiate)
on Jul 11, 2005 at 06:59 UTC ( #473850=perlquestion: print w/ replies, xml ) Need Help??
PonGopi has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am having a problem with this code
#!/usr/bin/perl $a = "a"; $b = "b"; $c = "c"; $d = "d"; $e = "e"; if (($a, $b, $c, $d, $e) eq ("ea", "eb", "ce", "de", "e")) { print "success"; } else { print "failure"; }
I dont know if we can give like this, but I was just trying to give if stmt in multiple ways.
What happens is if $e alone matches, it prints success and if $e alone fails, it prints failure.
Cant we use this method??
Should I modify the stmt using && operators...
any help is appreciated.
bye,
Gopi

Comment on Multiple check in If statement ??
Download Code
Re: Multiple check in If statement ??
by anniyan (Monk) on Jul 11, 2005 at 07:06 UTC

    You change it like this if you want to match everything else use || to satisy atleast one.

    You see perlop.

    $a = "a"; $b = "b"; $c = "c"; $d = "d"; $e = "e"; if (($a eq "a")&& ($b eq "b")&& ($c="c")&& ($d="d")&& ($e="e")) { print "success"; } else { print "failure"; }

    Regards,
    Anniyan

      if (($a eq "a")&& ($b eq "b")&& ($c="c")&& ($d="d")&& ($e="e")) {
      You have some = there that should be eq.
Re: Multiple check in If statement ??
by neniro (Priest) on Jul 11, 2005 at 07:06 UTC
    Hmm, you could concat all of them, so you just need to compare two strings and thats easy.

    As Thilosophy wrote, thats a bad idea so please ignore it.

      Hmm, you could concat all of them, so you just need to compare two strings and thats easy.

      That could also give a lot of false positives...

      $a = "abcd"; $e = "e";
        Yeah, you're right. Another try:
        #!/usr/bin/perl use strict; use warnings; my $a = "a"; my $b = "b"; my $c = "c"; my $d = "d"; my $e = "e"; if ( eq_list ([$a, $b, $c, $d, $e], [qw(a b c d e)]) ) { print "success\n"; } else { print "failure\n"; } sub eq_list { my ($arr1, $arr2) = @_; my @compared = map { $arr1->[$_] eq $arr2->[$_] ? 1 : 0 } (0..$#$a +rr1); my $bool = 1; $bool *= $_ for (@compared); return $bool; }
      This will not always work correctly without knowing a little more about the data.

      For example in your code, it will print success if $d='de' and $e = '' even though it should print false according to the OP's original intent. Depending on what you know about the data, that will be in those variables however, you could probably do what you are intending, but joining both sides with a common delimiter and then comparing them. For example:

      if ( join("->",$a,$b,$c,$d,$e) eq join '->',qw(a c c d e)) { print "success\n"; } else { print "failure\n"; }

      -enlil

        I hate to say it, but sometimes I type much faster than I think. Joining it using a delimiter is a much better idea than mine. I've added another code below thilosophies post that should be okay.
Re: Multiple check in If statement ??
by dirac (Beadle) on Jul 11, 2005 at 10:37 UTC
    my @j1 = ($a, $b, $c, $d, $e); my @j2 = qw(a b c d e); my $re = grep { $j1[$_] ne $j2[$_] } (0..$#j1); print $re ? "failure\n" : "success\n" ;

    Janitored by Arunbear - replaced pre tags with code tags, to allow code extraction.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-08-29 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls