Parsing an Array of Hashes

A

Artemis Fowl

Hello Experts,

I am dealing with an array of hashes in my program. I need to pick
every hash in the array, check a few keys and depending on it's value,
I need to either retain this hash or delete this hash from the array.
Please allow me to explain with a piece of sample code.

#!/usr/bin/perl -w
use strict;

my %MonthHash = ( "Jan", 0, "Feb", 1, "Mar", 2, "Apr", 3, "May", 4,
"Jun", 5, "Jul", 6, "Aug", 7, "Sep", 8, "Oct", 9, "Nov", 10, "Dec",
11, "Delete", "True");
my %WeekHash = ("Mon", 0, "Tue", 1, "Wed", 2, "Thu", 4, "Fri", 5,
"Sat", 6, "Sun", 7, "Delete", "False");
my %AgeHash = ("John", 21, "Susan", 23, "Gary", 18, "Christy", 20,
"Delete", "True");

my @arrayOfAll;
push(@arrayOfAll, %MonthHash);
push(@arrayOfAll, %WeekHash);
push(@arrayOfAll, %AgeHash);

I need to parse arrayOfAll, picking up each Hash turn by turn, and in
each of the Hashes, I want to check the value of the key "Delete". If
it is "True", then I want to delete the entire hash from the array.
Is there any way I could achieve this?

From what I see when I debug the script is that in arrayOfAll, the
details of different hashes are lost and all the key value pairs are
stored as just elements of the array.
Am I doing something wrong?
Any help will be greatly appreciated.

P.S : For the example, I have considered different Hashes. In the real
program, all the hashes have the same structure. Just thought this
information might help.

Thanks in advance,
Artemis.
 
P

Peter Makholm

Artemis Fowl said:
my @arrayOfAll;
push(@arrayOfAll, %MonthHash);
push(@arrayOfAll, %WeekHash);
push(@arrayOfAll, %AgeHash);

You have to store references to the hashes in you array. That is:

my @arrayOfAll;
push @arrayOfAll, \%MonthHash;
push @arrayOfAll, \%WeekHash;
push @arrayOfAll, \%AgeHash;

//Makholm
 
X

xhoster

Artemis Fowl said:
Hello Experts,

I am dealing with an array of hashes in my program. I need to pick
every hash in the array, check a few keys and depending on it's value,
I need to either retain this hash or delete this hash from the array.
Please allow me to explain with a piece of sample code.

#!/usr/bin/perl -w
use strict;

my %MonthHash = ( "Jan", 0, "Feb", 1, "Mar", 2, "Apr", 3, "May", 4,
"Jun", 5, "Jul", 6, "Aug", 7, "Sep", 8, "Oct", 9, "Nov", 10, "Dec",
11, "Delete", "True");
my %WeekHash = ("Mon", 0, "Tue", 1, "Wed", 2, "Thu", 4, "Fri", 5,
"Sat", 6, "Sun", 7, "Delete", "False");
my %AgeHash = ("John", 21, "Susan", 23, "Gary", 18, "Christy", 20,
"Delete", "True");

my @arrayOfAll;
push(@arrayOfAll, %MonthHash);
push(@arrayOfAll, %WeekHash);
push(@arrayOfAll, %AgeHash);

You don't have an array of hashes. You just have an array, with
alternating key value pairs. You need to push references to the hashes,
not the contents of the hashes:

push(@arrayOfAll, \%MonthHash); # etc
I need to parse arrayOfAll, picking up each Hash turn by turn, and in
each of the Hashes, I want to check the value of the key "Delete". If
it is "True", then I want to delete the entire hash from the array.
Is there any way I could achieve this?

@arrayofAll = grep {not exists $_->{Delete}} @arrayofAll;
From what I see when I debug the script is that in arrayOfAll, the
details of different hashes are lost and all the key value pairs are
stored as just elements of the array.
Am I doing something wrong?

Yes, see above about references.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
S

Sherm Pendley

Artemis Fowl said:
#!/usr/bin/perl -w
use strict;

my %MonthHash = ( "Jan", 0, "Feb", 1, "Mar", 2, "Apr", 3, "May", 4,
"Jun", 5, "Jul", 6, "Aug", 7, "Sep", 8, "Oct", 9, "Nov", 10, "Dec",
11, "Delete", "True");
my %WeekHash = ("Mon", 0, "Tue", 1, "Wed", 2, "Thu", 4, "Fri", 5,
"Sat", 6, "Sun", 7, "Delete", "False");
my %AgeHash = ("John", 21, "Susan", 23, "Gary", 18, "Christy", 20,
"Delete", "True");

my @arrayOfAll;
push(@arrayOfAll, %MonthHash);
push(@arrayOfAll, %WeekHash);
push(@arrayOfAll, %AgeHash);

I need to parse arrayOfAll, picking up each Hash turn by turn, and in
each of the Hashes, I want to check the value of the key "Delete". If
it is "True", then I want to delete the entire hash from the array.
Is there any way I could achieve this?

From what I see when I debug the script is that in arrayOfAll, the
details of different hashes are lost and all the key value pairs are
stored as just elements of the array.

That's because of how you're passing the hashes to push(). Pass
references instead, and you'll be good to go:

push(@arrayOfAll, \%MonthHash, \%WeekHash, \%AgeHash);

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top