On Fri, Aug 29, 2008 at 5:10 PM, Pedro Soto <[EMAIL PROTECTED]> wrote:
> 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);
>
> }
>
My solution to do the same :-
*# cat l1.pl*
#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $gh;
open (PTR, "<redundant_values.txt") or die "Cannot open input file : $!";
my $str;
while ($str = <PTR>)
{
chomp $str;
$str =~ s/ +/ /g;
my ($a, $b, $c) = split (/ /, $str);
# $gh =
# {
# 1 => {1 => 2},
# a => {b => c},
# p => {q => r}
# };
#
# $gh->{'$a'}->${'$b'} = $c;
print "==>$str\n";
if (! exists ${gh}->{"$a"}->{"$b"})
{
${gh}->{"$a"}->{"$b"} = $c;
# ${${${gh}}{"$a"}}{"$b"} = $c;
print Dumper($gh);
# print Data::Dumper->Dump($gh);
}
}
close (PTR);
print "Original File :-\n";
open (PTR, "<redundant_values.txt") or die "Cannot open input file : $!";
while ($str = <PTR>)
{
chomp $str;
print "$str\n";
}
close (PTR);
print "New File :-\n";
my ($a, $b);
while (($a, $b) = each (%{${gh}}))
{
my ($c, $d);
while (($c, $d) = each (%{${b}}))
{
print "$a,$c,$d\n";
}
}
*#*
*# perl l1.pl*
==>A 5 1
$VAR1 = {
'A' => {
'5' => '1'
}
};
==>A 5 1
==>A 5 1
==>A 6 1
$VAR1 = {
'A' => {
'6' => '1',
'5' => '1'
}
};
==>A 7 2
$VAR1 = {
'A' => {
'6' => '1',
'7' => '2',
'5' => '1'
}
};
==>B 5 8
$VAR1 = {
'A' => {
'6' => '1',
'7' => '2',
'5' => '1'
},
'B' => {
'5' => '8'
}
};
==>B 6 9
$VAR1 = {
'A' => {
'6' => '1',
'7' => '2',
'5' => '1'
},
'B' => {
'6' => '9',
'5' => '8'
}
};
Original File :-
A 5 1
A 5 1
A 5 1
A 6 1
A 7 2
B 5 8
B 6 9
New File :-
A,6,1
A,7,2
A,5,1
B,6,9
B,5,8
*#*
Regards,
Amit Saxena