Deb wrote:

> You know, I'm just not "getting it" I guess.  When I read through
> simple of examples in perldsc and perlfunc, it seems straightforward
> enough, but when I try to put into practice everything, it doesn't go
> as I might expect.
> 
> Recall this code I posted a day or two ago:
> 
> -------- 8-< -- snip --------
> 
> while (<DATA>) {
>     chomp;
>     ($listname, $field) = split(/:\s+/, $_);
>     print "\nListname is $listname,\nField is: $field\n";
>     %hrLists = split(/\s+/, $field);
>     $Lists{$listname} = \%hrLists;
> }
> 
> __DATA__
> list-1: -x abc -r tenb
> list-2: -x def -r ghi -h tenr
> list-3: -x fel -h asci
> list-4: -x foo -h nonasci -r bfab
> 
> -------- 8-< -- snip --------
> 
> Note the print in the while loop - essentially, when the hash is built, 
> my goal is to be able to use $listname as a key to call up the value of a
> key in the 2nd level hash.
> 
> I understand this:
> 
> foreach $listname (sort keys %Lists) {
>     print "$listname\n";
> }
> 
> But, I don't quite get how to get to the key values below that.  I know
> I'm so close, but just not quite there...
> 
> Could some kind soul give me a blow by blow "what's happening here" going
> from
> 
> $Lists{$listname} = \%hrLists;
> 
> to printing out the key, values by $listname?

There's nothing stopping you from doing something like the following. There 
really is no need to store a reference to a hash in the manner that you are 
doing so. 

#!/usr/bin/perl
use warnings;
use strict;
my %Lists;

while (<DATA>) 
{
    chomp;
    my( $listname, $field ) = split /:\s+/;
    print "\nListname is $listname,\nField is: $field\n";
    $Lists{$listname} = {
                (split /\s+./, $field)
        };
}

foreach ( sort keys %Lists )
{
        print "Key: $_\tReference: $Lists{$_}\n";
        print "Dereference:\n";
        foreach my $key ( sort keys %{$Lists{$_}} )
        {
                print "Inner Key: $key\tValue: $Lists{$_}->{$key}\n";
        }
        print "\n";
}

__DATA__
list-1: -x abc -r tenb
list-2: -x def -r ghi -h tenr
list-3: -x fel -h asci
list-4: -x foo -h nonasci -r bfab


Is this more understandable? 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to