On Monday 23,April,2012 01:27 AM, Jim Gibson wrote:
On Apr 22, 2012, at 9:52 AM, lina wrote:
Here is what I Have came up so far,
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw(open close);
use 5.012;
my $dict = system("tail -n 1 text_1.xvg");
Read the documentation on the system function. It does not return the output of
the child process to your program. For that, you need the qx() operator, or
backticks:
my $dict = qx("tail -n 1 text_1.xvg");
print $dict;
print "\n\n";
open my $fh, "<","text_2.xvg";
while(<$fh>){
print $_;
}
Of course, it is possible to read the last line of a file without resorting to
creating a child process. See, for example, the module File::ReadBackwards.
Thanks.
Here I came up a working script (unavoidably clumsy).
I don't know how to refine it, or make it terse.
Thanks ahead for your time,
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw(open close);
use 5.012;
open my $fh1, "<","text_1.xvg";
my $lastline;
my %dict;
my @lastline;
my $n;
while(<$fh1>){
$lastline = $_ if eof;
}
@lastline = split /[\t ]/,$lastline;
$n = shift @lastline;
foreach my $i (0..3){
$dict{$i}=$lastline[$i]
}
open my $fh, "<","text_2.xvg";
my @old;
while(<$fh>){
$n+=2;
my @old = split /\s*/,$_;
shift @old;
foreach my $item (@old){
s/$item/$dict{$item}/g;
}
print $n, "\t", join " ", @old;
print "\n";
}
$ more text_1.xvg
0 0 1 2 3
2 1 0 2 3
4 1 2 0 3
$ more text_2.xvg
0 0 1 2 3
2 1 0 3 2
4 1 3 0 2
output
$ ./renumber_v1.pl
6 0 1 2 3
8 1 0 3 2
10 1 3 0 2
Best regards,
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/