in reply to importing all the values stored in an HoH into an array

Updated: Added the omitted { words } term in the keys expression.

Update2: Added omitted paren as noted by johngg.

What you appear to be asking for is

my @wordsList; for my $word ( keys %{ $hash{ $w1 }{ $w2 }{ words } } ) { push @wordsList, ( $key ) x $hash{ $w1 }{ $w2 }{ words }{ $key }; } print @wordsList[ rand( @wordsList ) ];

There are probably bits of that you do not understand, but rather than my trying to guess which they are, it's easier if you ask. In this thread rather than starting a new one.

Note: I've omitted the -> between the terms of the hash references as they are optional and I personal don't think thay add clarity, but your way is fine also.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: importing all the values stored in an HoH into an array
by johngg (Canon) on Oct 31, 2006 at 12:12 UTC
    I think you might have a typo in your

    print @wordsList[ rand( @wordsList ];

    perhaps you wanted

    print @wordsList[ rand @wordsList ];

    Cheers,

    JohnGG

Re^2: importing all the values stored in an HoH into an array
by chinamox (Scribe) on Oct 31, 2006 at 13:37 UTC

    This worked like a charm, Thanks!

    Better yet, I think I can make some sense out of how the references work.

    one question: In the line:

    for my $word ( keys %{ $hash{ $w1 }{ $w2 }{ words } } ) {

    What does 'keys' stand for? Is it a reference?

    Thank you very much for your help!

    -mox
      keys is a function for pulling out all the keys from a hash. There is also a values function for ... er ... pulling out the values. You should also be aware of each. Here is an example.

      use strict; use warnings; my %dets = ( name => q{fred}, age => 33); while (my ($key, $value) = each %dets) { # Do something with key and value here }

      Note that you can't predict the order in which these functions will present keys, values or key/value pairs.

      I hope this is of use.

      Cheers,

      JohnGG

        Thank you for the example. It makes sense that there would be no predictable order for how keys, values, or key/value pairs are presented because a hash is not indexed like an array is. Still, these three tools will be most useful in my future dealings with hashes.

        cheers

        -mox
      What does 'keys' stand for? Is it a reference?

      'keys' is a Perl built-in function. It returns a list of the keys in a given hash. You should read the documentation (which I already pointed you to above).

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg