Parsing an Array of Hashes

Discussion in 'Perl Misc' started by Artemis Fowl, Sep 22, 2008.

  1. Artemis Fowl

    Artemis Fowl Guest

    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.
     
    Artemis Fowl, Sep 22, 2008
    #1
    1. Advertising

  2. Artemis Fowl <> writes:

    > 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
     
    Peter Makholm, Sep 22, 2008
    #2
    1. Advertising

  3. Artemis Fowl

    Guest

    Artemis Fowl <> wrote:
    > 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.
     
    , Sep 22, 2008
    #3
  4. Artemis Fowl <> writes:

    > #!/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--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Sep 22, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    593
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    186
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    295
    Matthias Wächter
    Sep 13, 2007
  4. Matt Brooks
    Replies:
    16
    Views:
    253
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    237
Loading...

Share This Page