http://www.perlmonks.org?node_id=1000445


in reply to Modify values of tied, split lines in a file

G'day glemley8,

"... I'm not sure how to modify just one variable of a split line using this method ..."

I've provided an example below of one way to do it. Do read all the caveats already provided. Benchmark may prove useful.

#!/usr/bin/env perl use strict; use warnings; use Tie::File; my ($file, $index, $replacement) = @ARGV; tie my @records, 'Tie::File', $file or die $!; for (@records) { my @fields = split /,/; $fields[$index] = $replacement; $_ = join ',' => @fields; } untie @records;

Example run:

$ cat fred A,B,C D,E,F G,H,I $ pm_tie_file_split_fields.pl fred 2 Z $ cat fred A,B,Z D,E,Z G,H,Z

-- Ken