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

Hi,
I have a hash constant like
use constant foo =>{ key1 => val1, key2 => val2 }

how I can iterate through the constant foo
while(($key.$val) = each(foo)) { print $key{$val}; }

when I specified key in each , it errors. Do someone know how I can dereference the hash constant foo.

when I print Dumper(foo), I got the folowing
Ref : HASH$VAR1 = { 'key1' => val1, 'key2' => val2 };
Thanks,
Tom

Replies are listed 'Best First'.
Re: Dereference a hash constant
by BrowserUk (Patriarch) on Oct 22, 2009 at 00:32 UTC

    Try it this way:

    use constant foo => { a .. z };; [0] Perl> print "$k : $v" while ($k,$v) = each %{ foo() };; w : x e : f a : b m : n s : t y : z u : v c : d k : l q : r g : h i : j o : p

    Or this:

    [0] Perl> print "$k : $v" while ($k,$v) = each %{ +foo };; w : x e : f a : b m : n s : t y : z u : v c : d k : l q : r g : h i : j o : p

    The 'trick' is to ensure that foo is parsed as a sub not a string or bareword.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      By the same reasoning:
      perl -e 'use constant foo => { a .. z }; print "$k : $v \n" while ($k +,$v) = each %{&foo};'
      And this works, but misses the point I guess:
      perl -e 'use constant foo => { a..z }; *foo_h=foo; print "$k : $v \n" +while ($k,$v) = each %foo_h;'
      Excellent solution. Thanks!!
Re: Dereference a hash constant
by bichonfrise74 (Vicar) on Oct 22, 2009 at 00:24 UTC
    Hmm... Can you try this?
    #!/usr/bin/perl use strict; use constant foo =>{ key1 => 'val1', key2 => 'val2' }; for my $i ( foo ) { for my $j (keys %{$i} ) { print "$j - $i->{$j}\n"; } }
      That works. Thanks much!!
Re: Dereference a hash constant
by chromatic (Archbishop) on Oct 22, 2009 at 00:22 UTC

    I don't believe you can make this work. You're defining foo as a hash reference. You'll have to figure out a way to dereference foo without making the parser think you're referring to a variable named %foo and how to avoid creating a new hash reference on every invocation. (I made a lovely infinite loop this way.)

    This assumes I even understand what you're trying to do; your example code makes little sense to me. What are you trying to do?

      I have a hash which has constant key/values, so I thought of making it as a constant hash. But now I realized it is impossible to iterate through a hash ref. Thanks.
        it is impossible to iterate through a hash ref

        Nuh uh! See 802571.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.