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

Hello monks, would someone please explain to me why this code isn't working. I can't seem to make sense of it
my $data = @size; if ($data >= 1) { print "MADE IT IN HERE\n"; } if (($freq eq 'FREE') && ($data >= 1)) { print "MADE IT INTO THE LOGICAL AND\n"; }
I'm able to get inside the first loop, but can't seem to get inside the two checks using && ( and yes the $freq is FREE

Thanks for the help

Replies are listed 'Best First'.
Re: Logical AND
by demerphq (Chancellor) on Nov 26, 2002 at 18:49 UTC
    Please to show us the code that sets $freq. Given the code here if it is 'FREE' and @size>=1 then both print statements should be output. So your assertion that $freq is actually 'FREE' is doubtful. Incidentally a more idomatic and easily readable way to write the above code is:
    print "Size array is not empty\n" if @size; print "The frequency is FREE!\n" if @size && $freq eq 'FREE';
    or if you intend to have more than one statement executed in the ifs
    if (@size) { #statements } if (@size && $freq eq 'FREE') { #statements }
    HTH

    --- demerphq
    my friends call me, usually because I'm late....

      If $freq is pulled out of a file it may have a \n on it as well you don't show us how it is set so this is just a shot in the dark but if you are splitting from a flat file to get $freq then you may want to chomp the line first to pull out the \n. In any case $freq ne 'FREE' =)

      -Waswas
Re: Logical AND
by jkahn (Friar) on Nov 26, 2002 at 18:45 UTC
    Looks okay to me:
    use warnings; use strict; my (@size) = (2,3,4); my ($freq) = 'FREE'; my $data = @size; if ($data >= 1) { print "MADE IT IN HERE\n"; } if (($freq eq 'FREE') && ($data >= 1)) { print "MADE IT INTO THE LOGICAL AND\n"; }
    And here's the output:
    D:\tmp>perl test.pl
    MADE IT IN HERE
    MADE IT INTO THE LOGICAL AND
    
    D:\tmp>
    
Re: Logical AND
by thewalledcity (Friar) on Nov 26, 2002 at 18:46 UTC
    I suspect that the variables are not really what you think they are. Try using print statements in the code to be completely sure what they are.
Re: Logical AND
by Anonymous Monk on Nov 27, 2002 at 00:18 UTC
    either reverse the if statements, or use an

    elsif

    in place of

    if

    for the second statement

    CH in NE PA