in reply to Re: naming a variable with the datum from another variable
in thread naming a variable with the datum from another variable

Okay, so that's bad way of doing it, but how do you do it because I have a working program that I just need to add a couple of loops into, and fiddling around with the data structures at this stage could be messy.

However, I would like to learn how to do it properly. I'm using an associative array already so that I can number the columns. Can I put an associative array within another associative array without all the data in them getting jumbled up?

  • Comment on Re^2: naming a variable with the datum from another variable

Replies are listed 'Best First'.
Re^3: naming a variable with the datum from another variable
by holli (Abbot) on Feb 12, 2005 at 18:02 UTC
    Can I put an associative array within another associative array without all the data in them getting jumbled up?
    Yes. That is a Hash of Hashes, HoH in short. see perlref and perlreftut. In fact you can create any kind of data-structure using perl-hashes and arrays. I donīt know if there is a "deepness"-limit but i doubt that.

    #sample set up a hash of hashes my %h = ( key1 => { subkey11 => subval11, subkey21 => subval21 }, key2 => { subkey12 => subval12, subkey22 => subval22 }, ); #access it print $h{key1}->{subkey11}; #loop it for ( keys %{$hash->{key2}} ) { print $hash->{key2}->{$_}; }
    Update:
    And yes, show us your code. But be sure to strip all non-relevant parts.

    holli, /regexed monk/
      I donīt know if there is a "deepness"-limit but i doubt that.

      Trust me - if there is, it's very, very large. I've had structures looking like join 'o', ('H') x dozens. 30+ deep. Even perl 5.6 could handle it. (Once they were all objects, perl 5.8 became required - 5.6 would crash.)

Re^3: naming a variable with the datum from another variable
by Tanktalus (Canon) on Feb 12, 2005 at 17:53 UTC

    I'm not entirely sure what you're trying to do - although here's a guess. It's not using hashes (associative arrays), but it could.

    use Data::Dumper; my $header = <DATA>; my @cols_h = split /\t/, $header; my @data; while (<DATA>) { chomp; my @info = split /\t/; push @data, \@info; } # uncomment this to see the data we have so far. #print Dumper(\@data); foreach (@data) { my $avg = ($_->[2] + $_->[3]) / 2; push @$_, $avg } # uncomment this to see the data we have so far. #print Dumper(\@data); # at this point, each @data is an array of arrays. # Each of the arrays contained in @data is "anonymous" (unnamed). # These anonymous arrays have the four columns (0-3), and a fifth colu +mn # which is an average of columns 2 and 3 (zero-based). __DATA__ ID_REF IDENTIFIER GSM29783 GSM29784 31307_at L17325 77 788 31308_at L17330 -777 -877 31309_r_at U50277 7 8 31310_at X52009 -7777 -887 31311_at X61070 77777 88