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

Hi all! I have a problem in getting values/keys from a dereferenced hash.
I get that hash by reading a file where it was written to before and evaluating it to a variable via eval():

my @file = <filehandle>; my $line = join "", @file; my $results = eval ($line);

I can access values (using a reference to that hash) when I manually look for a key in the file and use it in my code:

$test = $$results{"key"};

The problem is that I don't know the keys of that hash when reading from file since it's built at runtime with changing keys/values.
I can't get the keys by using each(), keys() or values() since these don't like references it seems.
Is there a possibility to evaluate that hash from the file to a real hash, or is there any way to get the keys from that dereferenced hash?

TIA, Michael
PS. My apologies for posting that topic before as anonymous, I forgot to enable cookies... ;-)

davorg: fixed code tags

Replies are listed 'Best First'.
Re: Getting keys/values from a referenced hash
by IraTarball (Monk) on Nov 27, 2001 at 20:55 UTC
    All you need to do is dereference the hash before passing it to keys or each. Try this:
    for my $key (keys %$results) { ... }

    Good luck!

    Ira,

      Chizz, it works! Thanks!!!
      Seems I have to learn a lot more about hashes than I know by now... ;-)

      Micha

Re: Getting keys/values from a referenced hash
by danboo (Beadle) on Nov 27, 2001 at 21:24 UTC
    Ira has already shown you how to access your keys so I thought I'd take a moment to suggest a more concise method of reading in your hash:
    my $hash = do 'hash.pl'; print $! || $@ || $hash->{'key'};
    See 'perldoc -f do' for more details on the effects of 'do FILE'. Cheers, - danboo
      Thanks, I'll give it a try!
      As I come from Pascal and Fortran77 (*yikes*) I still have some problems with the more advanced (say: efficient) way of accessing data in Perl.
      And I will need ages to be able to reply properly and not a thousand times like above... ;)

      Micha

dereferencing syntax Re: Getting keys/values from a referenced hash
by andye (Curate) on Nov 28, 2001 at 00:53 UTC
    Hi there professsa,

    In general, if you've got a reference to a hash - let's call it $ref - then you can work with it like this:

    #construct a ref to an anonymous hash: my $ref = {andy => 'likes cake'}; #note *curly* brackets #another way my %hash = (andy => 'likes beer too'); $ref = \%hash; #normal brackets + of course #get at one specific value print $ref->{andy}; #another way print $$ref{andy}; #get all the keys print keys %$ref; #get all the values print values %$ref; #get key/value pairs while (my ($key, $val) = each %$ref) {print "$key = $val"} #copy the hash into a new hash my %newhash = %$ref #if you use the -> syntax with references to references, then you can +omit the ->s, after the first one, like this: my $ref = { andy => {eats => 'cake'}}; print $ref->{andy}{eats}; #note only one -> necessary
    More info can be found here: perlref or here: References quick reference (or, newly, here if you speak German: (lang: de) Referenzen) or by typing 'perldoc perlref' at your command line.

    hth,
    andy.

    update: and the book "Effective Perl Programming" has an excellent explanation, very full, with pictures.

      Hi Andy,

      I got a question concerning dereferencing hashes and use strict;.
      I have a reference to a hash, derived from a reference to a nested hash:

      my $hashref = $nested->{"key_to_hash"};

      When I dereference that $hashref via

      line 35:   my %realhash = %{$hashref};

      and when I "use strict" I get the following message:

      Can't use an undefined value as a HASH reference at /home/user/programming/perl/perlscript.pl line 35 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors.

      Line 35 is the line where I dereference from $hashref.
      When I don't use strict everything works fine...I got no idea what means "defined value" here. All my refs and vars are defined, or am I wrong?

      Have a nice day and thanks for the tips yesterday!
      Micha

        Hi again Misha, here's my $.2,

        Well, this line (which I think is equivalent) works ok:

        perl -e 'my $nested = { key_to_hash => {data => "here"}}; print %{$nes +ted->{key_to_has h}};'
        The error message simply means that $hashref is undefined, i.e. the scalar called 'hashref' contains no value. So this means that there is a problem with $nested->{key_to_hash} - maybe the key key_to_hash doesn't exist, or the value is undefined. Maybe $nested is pointing to the wrong hash.

        An easy way to see what's gone wrong is to use the module Data::Dumper - which is standard, so you probably have it. Do this:

        use Data::Dumper; print Dumper($nested);
        and you'll see the contents of the hash referred to by $nested.

        If you can't see what's up with it, post the code where you set up $nested.

        Have a nice day and thanks for the tips yesterday!
        That's ok, you too.

        andy.

Re: Getting keys/values from a referenced hash
by IlyaM (Parson) on Nov 27, 2001 at 20:48 UTC
Re: Getting keys/values from a referenced hash
by Masem (Monsignor) on Nov 27, 2001 at 20:43 UTC
    How are you calling each/keys/values? If your hash is a reference, it should be something like keys %$results.

    You could also do :  my %real_hash = %$results which will attempt to copy your hash, but will not do a deep copy. If you need a better copy, you can use the Clone module to do that.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important