On 09/05/2011 21:29, D wrote:
Hello everyone,
I would like to learn an efficient way to change a single column in a
file that is accessed by an external program after the column is
changed each time. open write close is what I have been using. I
thought that tieing could help speed it up. While I didn't dig in too
deeply, my split entry, change value and rejoin didn't seem to gain me
much speed. The test file and script are pasted below. In practice
the file will be about 100 lines long and the 3rd column will be
rewritten thousands of times. Is there a more efficient approach?
example file:
'test'
-------------
foo ab 0
fooa b 0
foob cd 0
foo e 0
fooc f 0
foo ab 0
fooa b 0
foob cd 0
foo e 0
fooc f 0
-------------------------
script:
-------------------------------
use IO::All;
use warnings;
use strict;
my $lines = io('test')->new;
print "$_ \n" foreach @$lines;
print "\n\n\n\n\n";
my @tmp;
foreach (0 .. $#{$lines}){
$tmp[$_] = $_;
}
@$lines = map {
my @sh = split /\s+/, $lines->[$_];
join(" ",$sh[0],$sh[1],$tmp[$_]);
} 0 .. $#{$lines};
--------------------------
cat test:
foo ab 0
fooa b 1
foob cd 2
foo e 3
fooc f 4
foo ab 5
fooa b 6
foob cd 7
foo e 8
fooc f 9
foo ab 10
This looks like a job for a database. If you used SQLite there would be
no need to set up a server, and as very little data is being modified at
a time it should be lightning-fast.
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/