Home > Net >  Unable to get value from a nested Perl Hash Structure [closed]
Unable to get value from a nested Perl Hash Structure [closed]

Time:09-17

I've scoured the internet for guidance and have been working on this for days but cannot understand the data structure. I have a multilevel hash structure with nested arrays as well. It looks something like this

{'Suggestion' => {
     'Car' => {'Make' => ['Toyota'] }, 
     'Bike' => 'Giant', 
     'Motorcycle' => {'Make'=> ['BMW']}
};

I can extract Toyota and BMW to a string variable with {"Suggestion"}->{"Car"}->{"Make"} and {"Suggestion"}->{"Motorcycle"}->{"Make"}.

when I try to extract Giant using {"Suggestion"}->{"Bike"}, it gives me a HASH reference as a string (HASH(9879)). When I use Dumper({"Suggestion"}->{"Bike"}), it gives me $VAR = {'Bike' => undef};. So its a Hash with a key and undef value?

Then if I try to deference it using %({"Suggestion"}->{'Bike'}), it gives me an error:

Can't use string ("Bike") as a HASH ref while "strict refs" in use at (eval 7552). So this is telling me that I'm trying to use a string that I want as a hash ref? 

What am I doing wrong?

CodePudding user response:

Provided by OP sample code is not complete and curly brackets are unbalanced.

Bellow code snippet demonstrates that if data structure is complete there should be no any error.

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my $data = {
    'Suggestion' => {
         'Car' => {'Make' => ['Toyota'] },
         'Bike' => 'Giant',
         'Motorcycle' => {'Make'=> ['BMW']}
    }
};

say Dumper($data);

say 'Bike: ' . $data->{Suggestion}{Bike};

Output

$VAR1 = {
          'Suggestion' => {
                            'Bike' => 'Giant',
                            'Car' => {
                                       'Make' => [
                                                   'Toyota'
                                                 ]
                                     },
                            'Motorcycle' => {
                                              'Make' => [
                                                          'BMW'
                                                        ]
                                            }
                          }
        };

Bike: Giant

Sample to generate multiline string

my $trafficStr = "
Bike:       $Transport->{"Suggestion"}{"Bike"}
Car:        $Transport->{"Suggestion"}{"Car"}{"Make"}[0]
Motorcycle: $Transport->{"Suggestion"}{"Motorcycle"}{"Make"}[0]
";

CodePudding user response:

I was getting the data structure as an input. $Transport

This was the code snippet I was working with and is the final correct answer:

my $trafficStr = "";
$trafficStr .= "Bike: " . $Transport->{"Suggestion"}->{"Bike"} . "\n";
$trafficStr .= "Car: " . join(',' , @{$Transport->{"Suggestion"}->{"Car"}->{"Make"}}) . "\n" ;
$trafficStr .= "Motorcycle: " . join(',' , @{$Transport->{"Suggestion"}->{"Motorcycle"}->{"Make"}}) . "\n";
return $trafficStr;`

This will return:

Bike: Giant
Car: Toyota
Motorcycle: BMW

These were the WRONG ways to get Giant.

$trafficStr .= "Bike: " . join(',' , %{$Transport->{"Suggestion"}->{"Bike"}}) . "\n";
Commit failed: Can't use string ("Giant") as a HASH ref while "strict refs" in use at (eval 7552)
$trafficStr .= "Bike: " . join(',' , Dumper({$Transport->{"Suggestion"}->{"Bike"}})) . "\n";
Bike: $VAR1 = {'Bike'=>Undef}
$trafficStr .= "Bike: " . join(',' , $Transport->{"Suggestion"}->{"Bike"}) . "\n";
Bike: HASH(0x9f35c98)
  • Related