CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Accessing hash of array of arrays

 



sessmurda
Novice

Jan 5, 2011, 12:20 PM

Post #1 of 2 (626 views)
Accessing hash of array of arrays Can't Post

Hey all,

I posted this question in a more complicated form before and got no responses. After taking care of a few other things earlier this week I'm back at trying to finish this script. What I want to do is take the data below, and for each hash key, if there are multiple lines where column 4 is equal, and if in those same lines column 5 is equal (both instances also want to set a wobble of + or - 5). If for a hash, there are say 3 instances where the 2 columns give the same values (within the wobble range), then I want to exclude those. Basically I need to know the syntax to access specific elements of a hash of arrays of arrays. The file format is below.

TC356967 Scaffold4333 89.7818599311137 6443 7354 1
TC356967 Scaffold463974 89.3333333333333 7554 8067 -1
TC356967 Scaffold93112 89.3203883495146 7760 8068 1
TC356967 Scaffold174194 89.2733564013841 7781 8068 1
TC356967 Scaffold407210 89.0243902439024 7973 8054 1
TC356967 Scaffold13981 88.9487870619946 7553 8084 1
TC356967 Scaffold363276 88.8888888888889 7760 8083 1
TC356967 Scaffold214368 88.5844748858447 7554 8083 -1
TC356967 Scaffold299965 88.5245901639344 7781 8084 -1
TC356967 Scaffold221597 88.4615384615385 7554 8083 -1
TC356967 Scaffold192663 88.4507042253521 7729 8083 1
TC356967 Scaffold48509 88.4488448844885 7781 8083 1
TC356967 Scaffold88696 88.3668903803132 7554 8083 1
TC356967 Scaffold99960 88.2618510158014 7554 8083 1
TC356967 Scaffold1551 88.1944444444444 7551 8063 -1
TC356967 Scaffold14356 88.1481481481482 7554 8083 -1
TC356967 Scaffold148662 88.0733944954129 7760 8085 1

Code below.


Code
use warnings; 
use Data::Dumper;
open (IN, "$ARGV[0]") || die "nope\n";
my %data;
while(<IN>)
{
chomp;
my $line = $_;
my ($key, $scaf, $perc, $start, $stop, $strand) = (split/\t/, $line);
push @{ $data{$key} }, $line;

#foreach $key (keys %data)
#{
# my ($scaf1, $perc1, $start1, $stop1, $strand1) = @{$data{$name}};
#unless (

#}
}
print Dumper(\%data);



Data Dumper prints output like:
'TC383408' => [
'TC383408 Scaffold293195 100 1 132 1',
'TC383408 Scaffold45175 100 130 816 1'
],

Meaning that I am creating the hash of array of arrays fine, but I am unsure how to access the specific elements. Any leads on how to compare specific elements of a hash of arrays of arrays?

Thanks


FishMonger
Veteran / Moderator

Jan 5, 2011, 1:06 PM

Post #2 of 2 (623 views)
Re: [sessmurda] Accessing hash of array of arrays [In reply to] Can't Post

That's a HoA (hash of arrays), not a HoAoA (hash of arrays of arrays).


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my %data;

while ( my $line = <DATA> ) {
chomp $line;
my ($key, $scaf, $perc, $start, $stop, $strand) = (split/\t/, $line);

push @{ $data{$key} }, [ $scaf, $perc, $start, $stop, $strand ];
}

print Dumper(\%data), "\n";

foreach my $key ( keys %data ) {

print $data{$key}[0][1], "\n";
}


__DATA__
TC356967 caffold4333 89.7818599311137 6443 7354 1
TC356967 caffold463974 89.3333333333333 7554 8067 -1
TC356967 caffold93112 89.3203883495146 7760 8068 1
TC356967 caffold174194 89.2733564013841 7781 8068 1
TC356967 caffold407210 89.0243902439024 7973 8054 1
TC356967 caffold13981 88.9487870619946 7553 8084 1
TC356967 caffold363276 88.8888888888889 7760 8083 1
TC356967 caffold214368 88.5844748858447 7554 8083 -1
TC356967 caffold299965 88.5245901639344 7781 8084 -1
TC356967 caffold221597 88.4615384615385 7554 8083 -1
TC356967 caffold192663 88.4507042253521 7729 8083 1
TC356967 caffold48509 88.4488448844885 7781 8083 1
TC356967 caffold88696 88.3668903803132 7554 8083 1
TC356967 caffold99960 88.2618510158014 7554 8083 1
TC356967 caffold1551 88.1944444444444 7551 8063 -1
TC356967 caffold14356 88.1481481481482 7554 8083 -1
TC356967 caffold148662 88.0733944954129 7760 8085 1


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives