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

never mind, I got it... thanks anyway! \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ so I have a foreach loop:
foreach $line (<$filelog>){ chomp($line); @$ort_log[$site_index]=substr ($line, 2, 1); $DB::single=1; if ((scalar @$ort_log[$site_index])==scalar '+'){ @$filelog[$site_index]=substr ($line, 4, -1); $site_index=$site_index+1; }else{ @$filelog[$site_index]=(substr ($line, 4, -1))+4; $site_index=$site_index+1 } }
in the debugger, at the breakpoint, I use
x @$ort_log[$site_index]
to check if it is + or -. The debugger says that it is -. So why does it execute the
@$filelog[$site_index]=substr ($line, 4, -1); $site_index=$site_index+1;
instead of the else block? thanks

Replies are listed 'Best First'.
Re: if condition
by jwkrahn (Abbot) on Jun 25, 2008 at 02:56 UTC
    foreach $line (<$filelog>){

    Should be:

    while ( my $line = <$filelog> ) {

    And

    @$ort_log[$site_index]=substr ($line, 2, 1);

    Should be:

    $ort_log->[ $site_index ] = substr $line, 2, 1;

    And

    if ((scalar @$ort_log[$site_index])==scalar '+'){ @$filelog[$site_index]=substr ($line, 4, -1); $site_index=$site_index+1; }else{ @$filelog[$site_index]=(substr ($line, 4, -1))+4; $site_index=$site_index+1 }

    Should be:

    if ( $ort_log->[ $site_index ] eq '+'){ $filelog->[ $site_index ] = substr $line, 4, -1; $site_index++; }else{ $filelog->[ $site_index ] = substr( $line, 4, -1 ) + 4; $site_index++; }
Re: if condition
by kyle (Abbot) on Jun 25, 2008 at 03:00 UTC

    Here's the condition you're talking about:

    if ((scalar @$ort_log[$site_index])==scalar '+')

    The cause of your problem is that you're using "==" to compare strings, but it's only useful for comparing numbers. You need to compare with "eq" instead. With "==", you're comparing numeric values, which are both zero.

    Note also that scalar '+' is the same as '+'. It might also be good to write "@$ort_log[$site_index]" as "${$ort_log}[$site_index]" or "$ort_log->[$site_index]" to make it clear that $ort_log is an array reference. Your comparison would be better written:

    if ( '+' eq $ort_log->[$site_index] )

    Hope this helps.