Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Modify values of tied, split lines in a file

by kcott (Chancellor)
on Oct 23, 2012 at 06:35 UTC ( #1000445=note: print w/replies, xml ) Need Help??

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 $ fred 2 Z $ cat fred A,B,Z D,E,Z G,H,Z

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000445]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-03-18 00:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.