Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by kcott (Canon)
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000445]
help
Chatterbox?
[stevieb]: can anyone recommend a good IDE for C programming that has *proper* vim plugin support? I've tried Eclipse, Netbeans etc and just can't get anything to work properly on Linux. I'm open to paying, but not on some monthly/yearly plan (just a one-off cost)
[stevieb]: I'd go with CLion as it's very similar to what I use already (intelliJ for Perl, Pycharm for Python), but it's a recurring fee every year

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-01-18 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (163 votes). Check out past polls.