in reply to Re: Hash of refs to refs
in thread Hash of refs to refs

I get it! Keys are stringified. That's why. Ahhhh...
So, maybe what I could do is build an array of arrays:
my @samples = ( [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ], [ [ 1, 3, 5, 7 ], [ 2, 4, 6, 8 ] ] ); my $sample; foreach $sample (@samples) { my @set = @$sample; my @in = @{$set[0]}; my @out = @{$set[1]}; print "in: @in out: @out\n"; }
Or, if I really wanted to use the hashtable:
my %samples = ( 'a' => [ [1, 2, 3, 4], [5, 6, 7, 8] ], 'b' => [ [1, 3, 5, 7], [2, 4, 6, 8] ] ); my $key; foreach $key (keys %samples) { my @set = @{$samples{$key}}; my @in = @{$set[0]}; my @out = @{$set[1]}; print "in: @in out: @out\n"; }

The second method allows me to label my samples.
How nice!

Thanks again.

Rob

Replies are listed 'Best First'.
(jeffa) 3Re: Hash of refs to refs
by jeffa (Bishop) on Dec 01, 2001 at 20:24 UTC
    You got it!

    Welcome to the wonderful world of anonymous data structures, if you don't already know about it, Data::Dumper is a wonderful module (that comes with the standard distro) for debugging your stuctures to make sure that really have the composition you desire. Example:

    use Data::Dumper; print Dumper \%samples;
    Dumper will take a scalar, an array, a hash, references, and more - i find it best to give it references to hashes and arrays instead. Experiment and find out why!

    Also of interest to you might be Using stringified refs for unique IDs?.

    Last tip:

    # these 2 lines my $sample; foreach $sample (@samples) { # are usually better written as foreach my $sample (@samples) {
    The difference is that the $sample in the second snippet is local to the foreach block and goes out of scope when the foreach is finished. Since you aren't using $sample anywhere else it appears, i recommend you use this approach. But, if you do need $sample later in the code, use the first snippet.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)