http://qs1969.pair.com?node_id=445380

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

I'm dusting off my (very basic to begin with) perl skills for a little project; however, I'm having a bit of difficulty - it seems a while loop isn't exiting after reading a text file and inserting it into a multidimensional array - no errors at all, the process takes up 99% of CPU usage and just sits there.

From the "print" statements I inserted for troubleshooting purposes, it appears that the loop itself is in fact processing all the lines in the text file itself (which doesn't end on any special characters or anything, just a newline), and then goes mumble.

I'm not sure what's going on here, as for the most part I've been following various tutorials and the Camel Book when necessary.

I'm using ActiveState's build of v5.8.6 on Windows 2000 Pro, SP4.

Code is below; thanks for all suggestions - hopefully it's just something small I've overlooked.

open( BEST, "< $best_file") || die "Sorry, can't open $best_file: $!"; @best_array = (); @NewRow = (); while( <BEST> ) { print STDOUT "\t Reading next line...\n"; $Record = $_; chomp($Record); $Record =~ s/^\s+//; @NewRow = split(/\s+/, $Record); print STDOUT @NewRow[0], " ", @NewRow[1], "\n"; push( @best_array, [@NewRow] ); print STDOUT "\t Proceeding to next line...\n"; } print STDOUT "Processing BEST for duplicates"; ...

Replies are listed 'Best First'.
Re: while loop not exiting?
by RazorbladeBidet (Friar) on Apr 06, 2005 at 18:05 UTC
    Is there more code after the loop?

    Try adding a $|=1 to the top of your code. Just a hunch.

    Otherwise the code looks ok to me.
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      RazorbladeBidet means the loop may in fact be ending without you realizing it because of buffering. If select(STDOUT); $|=1; helps, it means the problem is actually after the loop, and that you got fooled into thinking it was within the loop because the printed data was still in a buffer.
      OK, thanks, that works. The next part of the code involves searching my newly created multidimensional array for duplicate entries but in other positions (and it's a fairly large text file to begin with), which takes a while, which is likely what I was experiencing while not seeing the next (apparently buffered) "print" statements.

      I have other questions about efficient searching/matching inside an array, but that's a subject for another thread and I have to go out now :)

      Thanks again!!

Re: while loop not exiting?
by polettix (Vicar) on Apr 06, 2005 at 18:46 UTC
    Even if this may not help you in this particular issue, I suggest running with strict and warnings enabled; warnings tell that you'll probably want to substitute:
    print STDOUT @NewRow[0], " ", @NewRow[1], "\n";
    with
    print STDOUT $NewRow[0], " ", $NewRow[1], "\n";
    to actually print the first two elements in @NewRow. I would also avoid using $Record:
    open( BEST, "< $best_file") || die "Sorry, can't open $best_file: $!"; my @best_array = (); # Silences strict while( <BEST> ) { print STDOUT "\t Reading next line...\n"; chomp; # Works on $_ s/^\s+//; # Ditto my @NewRow = split /\s+/; # Ditto, note scoping with my print STDOUT $NewRow[0], " ", $NewRow[1], "\n"; push( @best_array, \@NewRow ); # Take reference, avoid copy print STDOUT "\t Proceeding to next line...\n"; }
    Other than that, I tried your piece of code and runs smoothly in v. 5.8.5 in cygwin.

    Update: removed annoying hanging sentence

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.
Re: while loop not exiting?
by cog (Parson) on Apr 06, 2005 at 18:05 UTC
    That code works fine for me...

    Is there anything else in your script apart from that? Or perhaps something peculiar about $best_file we should know?