in reply to Print in TXT (while loop)

Thx for the replies. Ikegami gave me an easy solution. But isn't it easier to just use a for loop then? Or does it have disadvantages too?

Replies are listed 'Best First'.
Re^2: Print in TXT (while loop)
by ikegami (Patriarch) on Jul 25, 2008 at 13:26 UTC

    "I have a buffering problem, so I'll use 'for' instead of 'while'." I don't see the relation or the logic in that.

    If it's an independent question, then not only is it not easier (while (<$list_fh>) vs for (<$list_fh>) are equally easy to type), the for version needlessly uses more memory. for (<$list_fh>) causes the entire file to be read into memory before the loop starts whereas while (<$list_fh>) (short for while (defined($_ = <$list_fh>))) reads one line at a time.

Re^2: Print in TXT (while loop)
by parv (Parson) on Jul 25, 2008 at 09:23 UTC

    Like this ...

    for ( ; my $in = <$fh> ; ) { ... } # or ... for ( ;; ) { last if eof $fh; my $in = <$fh>; ... }

    ... ? The first case is very much like while with the additions of empty initialization & increment statements both of which happened to be covered by the loop end condition. So all you gain is obfuscation with for loop in this case.

Re^2: Print in TXT (while loop)
by Anonymous Monk on Jul 25, 2008 at 08:57 UTC
    No, it isn't, and it has disadvantages (reads whole file into memory at once)