Perl: Hash within Array within Hash

By : zmee

I am trying to build a Hash that has an array as one value; this array will then contain hashes. Unfortunately, I have coded it wrong and it is being interpreted as a psuedo-hash. Please help!

my $xcHash       = {};
my $xcLine;

#populate hash header

$xcHash->{XC_HASH_LINES} = ();

#for each line of data
    $xcLine       = {};

    #populate line hash

    push(@{$xcHash->{XC_HASH_LINES}}, $xcLine);

foreach $xcLine ($xcHash->{XC_HASH_LINES})
    #psuedo-hash error occurs when I try to use $xcLine->{...}
By : zmee


Here's what I would do:

my %xcHash;

for each line of data:

push @{$xcHash{XC_HASH_LINES}},$xcLine;

Golden Rule #1

use strict;
use warnings;

It might seem like a fight at the beginning, but they will instill good Perl practices and help identify many syntactical errors that might otherwise go unnoticed.

Also, Perl has a neat feature called autovivification. It means that $xcHash and $xcLine need not be pre-defined or constructed as references to arrays or hashes.

The issue faced here is to do with the not uncommon notion that a scalar can hold an array or hash; it doesn't. What it holds is a reference. This means that the $xcHash-

By : Zaid

foreach $xcLine ($xcHash-
By : ysth

This video can help you solving your question :)
By: admin