Inserting a new value into an already created file - Perl

advertisements

I have a file created which has data in columns 0, 1 and 2. I now have a new variable called $percentage that has 11 values associated to it that I wish to add to column 3 of the file.

How do I do this without appending to the bottom of the file?

Currently my data looks like, but would like it formatted next to the existing data:

title name number
title name number
title name number
title name number
                  $percentage value 1
                  $percentage value 2
                  $percentage value 3
                  $percentage value 4

etc


use Tie::File;

#! /usr/bin/env perl
use common::sense;
use Tie::File;

tie my @f, 'Tie::File', 'foo' or die $!;

my $n;
for (@f) {
  $_ .= ' $percentage value ' . $n++;
}

untie @f;

Example:

$ cat foo
title name number
title name number
title name number
title name number
$ perl tie-ex
$ cat foo
title name number $percentage value 0
title name number $percentage value 1
title name number $percentage value 2
title name number $percentage value 3