in reply to efficient Array printing to a file

First try wrapping your sample code in code tags to make it readable, so it looks like this:
foreach $x (@x) { open ($x, ">>$log") or die; print while (<$x>); printf "%7d",$x; } close ($x);
A more common idiom for printing to a file would be this:
open (OUTPUT, ">>$log") or die "Error opening file $log ($!)\n"; my $line = join ' ', @x; print OUTPUT $line, "\n"; close (OUTPUT) or die "Error closing file $log ($!)\n";
Catching possible errors from your system IO operations in $! is generally a good practice. Also, the version I showed doesn't reopen the filehandle every time you want to print a array element to the file. The join operator is very useful, do a perldoc -f join if you've never seen it before. A perldoc -f open would probably be helpful to you as well.

Hope that this helps. :)

Replies are listed 'Best First'.
Re: Re: efficient Array printing to a file
by Anonymous Monk on May 27, 2004 at 23:01 UTC
    Hi there!!, Thank you for a very quick reply...that indeed is a very good suggestion..and it it works fine too..I wanted to try "printf" so that I have more control over the output. :-{>