diarmuid has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've started messing around with references today and I have a few questions.
I have a hash of a hash %clocks (eg $clock{<name>}{'speed'}) Now I want to read the outer hash in a subroutine. I was reading on references and I think that's what I should use, but I'm not sure of the syntax.
eg

foreach my $key (sort keys %clocks){ print_clock_assertions(\{ $clocks{'$key'} }); } sub print_clock_assertions { my($hashref)=$_[0]; print STDOUT "ref 1 :$hashref->{'speed'}\n"; }

This doesnt work so if you know what I'm at could you point me in the right direction?

Diarmuid

Edit: chipmunk 2001-04-19

Replies are listed 'Best First'.
Re: hashes,references and subroutines
by jeroenes (Priest) on Apr 19, 2001 at 16:46 UTC
    diarmuid You're nearly there. Try to understand that a hash-of-a-hash is really a hash-of-hashreferences.

    If you leave out the referencing in line 2, you are fine (alternatively, do a double dereferencing in the sub).

    Hope this helps,

    jeroen
    "We are not alone"(FZ)
    Update: Forgot some ObDocLinks: perlref, perlreftut,perldsc and perllol
    Use CODE tags around your code:
    <CODE>
    #my perlcode may contain any character like [] </CODE>
    Read Writeup Formatting Tips

      OK, I'm still not sure what's going on.
      I ran

      foreach my $key (sort keys %clocks){ print_clock_assertions({ $clocks{$key} }); #line 32 } sub print_clock_assertions { my($hashref)=$_[0]; print STDOUT "ref 1 :$hashref->{'speed'}\n"; #line 44 }
      and got loads of
      Odd number of elements in hash assignment at parse_timing_info.pl line + 32. Use of uninitialized value in concatenation (.) at parse_timing_info.p +l line 44. ref 1 :
      does $hashref contain the has reference when I do this?

      PS Why how do stop the square bracket being removed?

      Edit: chipmunk 2001-04-19

        You have to remove the {} in line 32.

        PS: Put your code in <code>tags.

Re: hashes,references and subroutines
by Anonymous Monk on Apr 19, 2001 at 17:07 UTC

    Lets first deal with the main code. You wrote:

    foreach my $key (sort keys %clocks){ print_clock_assertions(\{ $clocks{'$key'} }); }

    By putting single quotes around $key, you are now passing literally the text $key to the subroutine so you need to remove those single quotes as in:

    foreach my $key (sort keys %clocks){ print_clock_assertions(\{ $clocks{$key} }); }

    Next, as I understood you you had has that contained references to hashes that contained clock information. In which the act of writting $clocks{$key} would return a reference to a clock hash, so you don't need to enclose the that in \{ }. So the next revision is:

    foreach my $key (sort keys %clocks){ print_clock_assertions($clocks{$key}); }

    Your subroutine looks fine, though I always have problems discerning when perl will recognize something as an expression in double quotes or not. So if you do have problems change:

    print STDOUT "ref 1 :$hashref->{'speed'}\n";

    To:

    print STDOUT "ref 1 :", $hashref->{'speed'}, "\n";
    Cheers...james
Re: hashes,references and subroutines
by mr.nick (Chaplain) on Apr 19, 2001 at 16:46 UTC
    You should be able to emumerate the values of %clock like this:
    for my $key (sort keys %clocks) { for my $subkey (sort keys %{$clocks{$key}}) { print "clocks/$key/$subkey = $clocks{$key}{$subkey}\n"; } }
Re: hashes,references and subroutines
by diarmuid (Beadle) on Apr 19, 2001 at 18:40 UTC
    thanks to all....