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

Hello guys , I have this pice of code :
while ($theSubBuild ne 'sci.bld' || 'esel.bld' ){ system (qq(grep $theSubBuild nbsssbs//esel/bld/*.bld nbsssbs/sbc/bld/ +*.bld > $tempDir/otherblds3.txt)); open(FileOut2, "< $tempDir/otherblds3.txt") || die "Could not open the file : $!\n"; my @final2; my @end2; my $firstBld2; my $secondBld2; while (<FileOut2>) { push(@final2, $1)&& last if (m(\w+\.bld:\w.*)/ ); } my $finalValue2 = join(' ', @final2); ($theSubBuild, $secondBld2) = split (/:/, $finalValue2); close(FileOut2) or die("Can't close file: $!"); }
I can't exit the loop , I get the value of $theSubBuild equal to sci.bld or esel.bld but It keep looping and losing that value .. thanks for help

2002-07-10 Edit by Corion : Added CODE tags

Replies are listed 'Best First'.
Re: exit the loop corrected
by lemming (Priest) on Jul 10, 2002 at 22:12 UTC

    while ( $theSubBuild ne 'sci.bld' || 'esel.bld')
    is equivalent to
    while ( $x ne 'ya' || 1 )

    I assume you meant for the loop to stop if $theSubBuild is equal to either of your strings. What Perl is doing is seeing if either of the expressions on both sides of the || are true. 'esel.bld' will always be true.

    What you probably want is:
    while ( $theSubBuild ne 'sci.bld' && $theSubBuild ne 'esel.bld')

    Update: Fixed a bit of spelling and added comments since I was editing anyways.

    I'm uneasy about the grep system call into a file and then reopening the file. I'd rather just read from a pipe if I was going to use the system grep through a bunch of files.

    I tend to just read the files and get my info that way and leave the OS out of it.

Re: exit the loop corrected
by DamnDirtyApe (Curate) on Jul 11, 2002 at 00:05 UTC
Re: exit the loop corrected
by krisahoch (Deacon) on Jul 10, 2002 at 22:57 UTC
    Amoura,

    Please, always use strict;. It may take you extra time to get something working, but it will be well worth it. Another thing you may want to do is use diagnostics;. This will give you more insight as to what the problem is.

    Would you please post an example file in your scratch pad? It doesn't have to be real, just an idea of what to expect.

    Thank you, Kristofer
Re: exit the loop corrected
by bronto (Priest) on Jul 11, 2002 at 12:33 UTC

    Your while condition is equivalent to: (($theSubBuild ne 'sci.bld') or 'esel.bld'), which is not what you want, I believe...

    You could use a regexp, like:

    while ($theSubBuild !~ /^(sci|esel)\.bld$/) {

    ...or split your condition in two subconditions:

    while (($theSubBuild ne 'sci.bld') or ($theSubBuild ne 'esel.bld')) {

    I can't tell you which one is the best. Is efficiency is not critical, choose the one you can mantain best :-)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }