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

Hi, I need help with a program I'm trying to right where I need to get specific values from a hash table and return them. I'll try my best to explain where I have to pull these elements from. The hash table has the structure:
%hash -> $variable_name -> 'fixed name(s)' -> value
I'm trying to assign the "value" to a scalar and return it but so far I'm not having any luck. $variable_name is being generated by a foreach loop and is definitely working. I've been trying code along the lines of
my $value = values %{$hash{$variable_name}{'fixed name'}}; print $value;
But this gives me "0". Which it's not. print Dumper %hash shows a nice hash table containing all the data it should so I know it's there. Can someone tell me what I'm doing wrong here?

Replies are listed 'Best First'.
Re: How to get specific hash elements?
by kyle (Abbot) on May 14, 2007 at 15:35 UTC

    First, values returns a list of values, not just a single value. You're assigning it in scalar context, so what you're getting is the number of values that it would return if you'd used it in a list context. That said, I suspect that values isn't even what you want to be using.

    If you do an assignment like this (I can't see the foreach loop you say is working):

    $hash{$variable_name}{'fixed name'} = 'val-you';

    ...then you can get that value out with the same expression you used in the assignment ($hash{$variable_name}{'fixed name'}).

Re: How to get specific hash elements?
by MonkE (Hermit) on May 14, 2007 at 15:39 UTC

    If I understand you correctly, you should be able to use $hash{$variable_name}{'fixed name'} both as an lvalue or an rvalue.

    Try this (tested):

    use strict; my %hash; $hash{'test1'}{'fixed name'} = 42; my $variable_name = 'test1'; print $hash{$variable_name}{'fixed name'}; print "\n";

    When executed, it prints 42.

      Tried this format and it's giving me "Use of uninitialized value in print at ./testScript.pl line 10". My hash is actually of the form:
      $VAR1 = 'Variable_name1 '; $VAR2 = { 'Fixed_name1 ' => { 'value ' => undef }, 'Fixed_name2 ' => { 'value ' => undef } }; $VAR3 = 'Variable_Name2 '; $VAR4 = { 'Fixed_name1 ' => { 'value ' => undef }, 'Fixed_name2 ' => { 'value ' => undef } };
      I don't know why it's pointing the values to undefined at the end. "value" is the thing I'm looking for. Thanks for the help guys. Been stuck on this for a few days and it's beginning to annoy me

        What's with all the white space at the end of your keys? That's going to cause problems: 'Fixed_name1     ' does not equal 'Fixed_name1'.

        -derby

        Ah. Well, clearly, all the values of the "leaf" nodes in the structure are undef. What you want is the keys of the bottom level of the struct. E.g.:

        my @values = keys %{ $hash{'test1'}{'fixed name'} };
        A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: How to get specific hash elements?
by jdporter (Paladin) on May 14, 2007 at 15:33 UTC

    Still not clear. Try posting an illustrative bit of the dump, and maybe some code that populates the structure. TIA.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: How to get specific hash elements?
by derby (Abbot) on May 14, 2007 at 15:33 UTC

    Is 'fixes name' one hash key or two? values returns all the values of a hash.

    -derby