El Friday 29 August 2008 13:40:04 Pedro Soto va escriure: > Hi, > I am trying to write script to retrieve info from a file that looks like: > > col1 col2 col3 > A 5 10 > A 5 10 > A 5 11 > A 6 8 > A 7 9 > B 5 8 > B 6 9 > what i need is to get for each (non redundant) value from column 1, the > corresponding non redundant values from column 2 and 3. e.g: > For A (col 1), I want 5 -10, 5-11 and 6-8. For B: 5-8 and 6-9. > I wrote a script to get rid of the redundant values using hashes and > subroutines and it worked. However I still need to compare the elements > from col2 and col3 with other values. To do this I want to sort the data, > but I am struggling to sort the hash. It prints what I want but only if ask > it to print within the subroutine (line 29). I do not know how to return a > hash with the sorted values. I hope someone could help me out with this. > The code is below: > > > #! usr/local/bin/perl > > use warnings; > > use strict; > > my %db_del; > > my %std_dup; > > open(IN,"file.csv") || die; > > while (<IN>) { > > my @temp=split/,/; > > push (@{$db_del{$temp[0]}}, $temp[1]."\t".$temp[2]); > > } > > &NONRE(%db_del,%std_dup); > > > > foreach my $e(%db_dup) { > > foreach my $l (@{$db_dup{$e}}) { > > print "$e,$l,$std_dup{$l}\n"; #does not print $std_dup{$l} > > }} > > > > ########sub############## > > sub NONRE { > > my %hash; > > my %seen; > > my @uniq; > > my %st; > > %hash = @_; > > foreach my $k (sort keys%hash) { > > foreach my $item(@{$hash{$k}}) { > > push(@uniq,$item) unless $seen{$item}++; > > } > > foreach my $item(@uniq) { > > my @stend =split/\t/,$item; > > $st{$stend[0]}= $stend[1]; > > } > > @{$hash{$k}}= sort {$a <=> $b} keys%st; > > foreach my $f(keys%hash){ > > foreach my $l(@{$hash{$f}}) { > > print "$f,$l,$st{$l} ok\n";# it prints OK > > } > > } > > } > > @uniq =(); > > %seen =(); > > return(%hash,%st); > > }
To sort a hash do: foreach $key (sort keys %hash) { print "$key: $coins{$key}"; -- Xavier Mas ______________________________________________ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y m�viles desde 1 c�ntimo por minuto. http://es.voice.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/