Sheograph
Sheograph

Reputation: 77

Perl: Composition of a Variable of Hashes

I have to parse a text file and put the information together. My main problem is this data structure inside the external text file:

# Text file 
$basket = [
          {
            'Apple' => 'red',
            'Banana' => 'yellow',
            'Grapes' => 'purple'
          },
];

print "$basket[0]{'Apple'}\n";

Obviously I am getting an error message as this is neither an array nor a hash. Still I need to print out the Values to my keys. But this only works when using my own code:

# My input #
my %fruits = (
             'Apple' => 'red',
             'Banana' => 'yellow', 
             'Grapes' => 'purple');

print "$fruits{'Apple'}\n";

Does anyone have a clue how to access the value 'red' when referencing the text file.

Upvotes: 1

Views: 72

Answers (1)

Dave Cross
Dave Cross

Reputation: 69264

What you have there is an array reference, not an array. So you need to use an extra piece of syntax (an arrow, ->) to dereference it.

$basket->[0]{Apple}

See perldoc perllol and perldoc perldsc for more information.

Upvotes: 4

Related Questions