Hi,
I tried many different ways to resolve it, even if I am still a newbie
in perl. Don't be cruel
It is not my intention to waste my and your time.
The problem is a bit more complicated. I have a datafile like that:
ala21,-0.03
thr43,0.06
phe124, 0.01
......
sorted for the number of the first column (21<43), and then I have a
series of files from which I need to parse some data, which are "linked"
to the first column of the datafile. The problem is that in those files
the sequence of the data is unsorted:
first file:
560 <other numbers> -0.04
54 ................. 0.02
235 ................ 0.01
..............
second file:
560 <other numbers> -0.01
54 ................. 0.05
235 ................ 0.03
third file:
560 <other numbers> -0.08
54 ................. 0.1
235 ................ 0.11
and so on.
basicaly:
aa num shift_1 shift_2 shift_3 ....
ala21 -> 54 -> 0.02 0.05 0.1
and so on.
The only way to do it (for me..) is to make an hash between the first
value of the datafile (ala21) with the smallest value of the files, the
second value (thr43) with the second smallest value of the files, and so on.
for doing this I need first of all sort by the key of the second hash
obtained from the files, and then link to datafile
have a look to my script:
#!/usr/bin/perl
use warnings;
use strict;
die "USAGE : $0 shiftfile number\n" if ($#ARGV!=1);
my (@tmp1,@tmp,@aa,@cis,%NEW);
my ($num,$shift,$i);
my $count=0;
open (SFT,"$ARGV[0]") or die "$!";
open (OUT,">tmp.csv") or die "$!";
while (my $line=<SFT>){
chomp $line;
@tmp=split(/,/,$line);
chop $tmp[1];
push @aa,$tmp[0];
push @cis,$tmp[1];
}
foreach my $file (1 .. $ARGV[1]){
open (IN,"rank_$file\.sft") or die "$!";
print "rank_$file\.sft\n";
while(my $lines=<IN>){
chomp $lines;
if($lines=~ /^ Atom number expt ring sp2anis
Efield total/){
$i=0;
while(my $nlines=<IN>){
chomp $nlines;
@tmp1=split(/ +/,$nlines);
$num=$tmp1[1];
$shift=$tmp1[11];
push (@{$NEW{$num}}, $shift);
++$i;
}
}
}
++$count;
}
my $j=0;
foreach my $index (sort {$a<=>$b} keys %NEW){
# print OUT "@{$NEW{$index}} $sft[$j]";
print OUT "$aa[$j] $cis[$j] @{$NEW{$index}}\n";
++$j;
}
print "$i cis and $count files done\n";
print "tmp.csv created. Open it with OOo using space and comma as
separator\n";
That works quite well for giving my what I want. The problem is then if
I want to "transpose" (thanks Arndt) the matrix obtained from that script.
A good idea was to make:
push (@{$TMP{$file}}, $shift};
but the $shift are not linked to the $num, so I cannot link with the
data from datafile anymore.
That why I said hash of hash (or maybe I am still wrong?)
I will try to use your suggestions in the script above.
thanks
regards
xspirix