> -----Original Message-----
> From: Chas. Owens [mailto:[EMAIL PROTECTED] 
> Sent: Friday, January 18, 2008 9:07 PM
> To: Kevin Viel
> Cc: beginners@perl.org
> Subject: Re: Hash of hashes?
> 
> On Jan 18, 2008 6:06 PM, Kevin Viel <[EMAIL PROTECTED]> wrote:
> > This I cannot get my mind around...
> >
> > My data:
> >
> > SNP  Genotype
> > 1       CC
> > 1       CT
> > 1       TT
> > 1       NN
> >
> >
> > It seems to me that I need a hash of hashes.
> >
> > Inner hash:
> >
> > $inner{ $Genotype }++ ;
> >
> > Since the value of the out hash ( $outer{ $SNP } ) has to 
> be a scalar, 
> > this scalar has to be a reference to the inner hash, 
> correct?  If so, 
> > how do I declare them?
> >
> > my %outer ;
> snip
> 
> In general you only declare the outer hash.  Perl uses some 
> magic called autovivication to create the inner hash references.
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use Data::Dumper;
> 
> my %data; #pick a better name
> while (<DATA>) {
>     my ($snp, $genotype) = split;
>     $data{$snp}{$genotype}++
> }
> print Dumper(\%data);
> 
> __DATA__
> 1       CC
> 1       CT
> 1       TT
> 1       NN
> 2       CT
> 2       CT
> 2       CT

Thank you for your help.


>     $data{$snp}{$genotype}++

Is the semicolon unnecessary for this line?

So, if I understand correctly  $data{$snp} is a value in a hash.  That value
is a scalar that happens, in this case, to be a reference to an anonymous
hash?  The key of this anonymous hash is $genotype?  It does not seem like
the simplicity of this lines relates the complexity of the object:

$data{ $snp }
$data{ $snp }{ $genotype }


####

Consider the code below:

#! /usr/bin/perl

use strict ;
use warnings ;

use Data::Dumper ;

my %outer ;

while ( <DATA> ) {
  my ( $snp , $genotype ) = split , /\s+/ ;
  my $allele1 = substr $genotype , 0 , 1 ;
  $outer{ $snp }{ $allele1 }++ ;
  my $allele2 = substr $genotype , 1 , 1 ;
  $outer{ $snp }{ $allele2 }++ ;
}

print Dumper( \%outer ) ;
 
__DATA__
1 CC
1 CT
1 TT
1 NN
2 CC


#################

$VAR1 = {
          '1' => {
                   'T' => 3,
                   'N' => 2,
                   'C' => 3
                 },
          '2' => {
                   'C' => 2
                 }
        };


My goal is to print all of the SNP (keys of outer) that have more than two
alleles (keys of the second anonymous hash).  How can I achieve this?

for my $SNP_keys ( sort { $a cmp $b ) keys %outer ){

  my $num_alleles = keys _______ ;    

}

The values of outer, for instance $outer{ $num_alleles }, are scalar
(references to anonymous hashes).  However, I cannot seem to dereference
them.

I appreciate any help.

Thank you,

Kevin


Kevin Viel, PhD
Post-doctoral fellow
Department of Genetics
Southwest Foundation for Biomedical Research
San Antonio, TX 78227  
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to