in reply to How can you sysread an entire file?
sysread(DF, $rec, -s DF); will save you from specifying an arbitrary number.
foreach(split("\n",$rec)){push(@array,"$_\n";)} causes three copies of the file to be in memory at once. ($rec, foreach's list and @array).
push(@array, $1) while $rec =~ /(.*?\n)/g; will save you from having an third copy of the file in memory, and will save you from re-adding the "\n". Caveat: It will skip the last line if it isn't terminated by a "\n".
Unfortunately, splitting on "\n" is not portable. Keep in mind that sysread has a bug causing it to never translate CRLF to LF in Windows.
Is push(@array, $_) while <DF>; really that much slower?
Any why not use undef $rec; instead of $rec = '';? I prefer to just use curlies, though: my @array; { my $rec; ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How can you sysread an entire file?
by davidrw (Prior) on Jan 12, 2006 at 20:24 UTC | |
by NeilF (Sexton) on Jan 12, 2006 at 22:27 UTC | |
by ikegami (Patriarch) on Jan 13, 2006 at 22:14 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2006 at 23:15 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2006 at 22:54 UTC | |
Re^2: How can you sysread an entire file?
by NeilF (Sexton) on Jan 12, 2006 at 22:23 UTC | |
by esskar (Deacon) on Jan 12, 2006 at 23:14 UTC | |
by NeilF (Sexton) on Jan 13, 2006 at 18:33 UTC | |
by ikegami (Patriarch) on Jan 13, 2006 at 22:10 UTC | |
by NeilF (Sexton) on Jan 13, 2006 at 23:11 UTC |