deb wrote:

> I'm not sure what you're saying.  Since this is an anonymous hash assignment,
> how do I pull out the $listkey?  Do I need to pre-assign it?  For example,
> I tried this, added to the previous program,
>
> foreach $listname (sort keys %Lists) {
>     print "$listname:\n";
>     foreach my $key (sort keys %{$Lists{$listname}}) {
>         print "\t$key:  ${$Lists{$listname}}{$key}\n";
>     }
> }

Try something like the changes above.  The clue that something was wrong with the way 
you had it set up was that you foreach'ed on the same array in the inner loop as the 
outer.  In a nested loop structure, the inner loop should build on the work done by 
the outer.  Here, the outer loop directs you to the hash representing a given command 
line.  The reference to that hash is the element $Lists{$listname}.  This is 
equivalent to the bare name of the hash--without the %.  So to get the keys hash, you 
should hashify the reference by prepending the %:
%{$Lists{$listname})}   # NOTE braces
Likewise, you can get the second level, element by prepending the scalar symbol $ and 
citing a key.
${$Lists{$listname}}{$key}

Thanks.  This exercise taught me a few things about dereferencing.  Particularly to 
enclose a reference such as $Lists{$listname} in braces, rather than parens, to get at 
the payload.
The line:
        print "\t$key:  ${$Lists{$listname}}{$key}\n";
could also be written:
        print "\t$key:  $Lists{$listname}->{$key}\n";
I prefer the first myself, because I think it communicates more directly the logic of 
the dereference.  That is totally a matter of taste, though.  TIMTOWTDI.

Joseph

Here's what I drew out of this note the use strict and adaptations to satisfy 
compilation rules:

#!/usr/bin/perl -w

use strict;

my %Lists;

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

foreach my $listname (sort keys %Lists) {
    print "$listname:\n";
    foreach my $key (sort keys %{$Lists{$listname}}) {
        print "\t$key:  ${$Lists{$listname}}{$key}\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

OUTPUT:
E:\d_drive\perlStuff\guests>deb2dhash.pl

Listname is list-1,
Field is: -x abc -r tenb

Listname is list-2,
Field is: -x def -r ghi -h tenr

Listname is list-3,
Field is: -x fel -h asci

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

E:\d_drive\perlStuff\guests>


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

Reply via email to