Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by kcott (Abbot)
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 $ pm_tie_file_split_fields.pl fred 2 Z $ cat fred A,B,Z D,E,Z G,H,Z

-- Ken


Comment on Re: Modify values of tied, split lines in a file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000445]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2015-07-29 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls