in reply to How to get specific hash elements?

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.

Replies are listed 'Best First'.
Re^2: How to get specific hash elements?
by talking_walnut (Initiate) on May 14, 2007 at 15:56 UTC
    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
        I figured that alright and tried the code with all the spaces but it didn't make any difference. I'm pulling the data from a database that's why the white space is there. That's also how it's being assigned the undef value although I don't know why. Here's the structure of the code that's doing that:
        my %ret; if ( defined( $rv ) and @$rv ) { map { %{$ret{$$_{name}}->{$$_{project}}={$$_{result}}} +} @$rv; } return %ret;

        Update: I finally managed to get this working the way I wanted. Just thought I'd post how I did it in case it's useful to someone else in the future and wanted to say thanks to everyone that took the time to reply to me. Here's the approach I took:
        my ($value1, $value2); foreach (sort keys %hash) { foreach (sort keys %{$hash{$random_name}}) { foreach $k1 (sort keys %{$hash{$random_name}{$fixed_name1}}) { $value1 = $k1; } foreach $k2 (sort keys %{$hash{$random_name}{$fixed_name2}}) { $value2 = $k2; } etc. } } print "$value1\n";
        Worked a charm. My full version has some error checking in case one of the values didn't exist. Did this with some if defined before the foreach and an else { after.

      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
        But is it possible to have a scalar for each "fixed_name" assigned the value of "value"? I suppose a better representation of the hash would be:
        $VAR1 = 'random_name '; $VAR2 = { 'Fixed_name1 ' => { 'value ' => undef }, 'Fixed_name2 ' => { 'value ' => undef } }; $VAR3 = 'random_Name '; $VAR4 = { 'Fixed_name1 ' => { 'value ' => undef }, 'Fixed_name2 ' => { 'value ' => undef } };
        The fixed names are always going to be the same but there will be hundreds of random names. I want to return the value of the fixed name to another function that will display this value on a webpage for each random name. There will only ever be 1 element for value. I hope I'm explaining this better each time and not just making it twice as confusing :-)