Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:16:43+00:00 2026-06-05T12:16:43+00:00

I am using the the below Array of Hashes and would like to only

  • 0

I am using the the below Array of Hashes and would like to only display the most recent hash based off the ‘Datetime’, if four certain parameters are the same. Let me provide an example using the code below…

If ‘toy, kind, Stage, Step’ are the same, then I would only like to store only that hash into a new array of hashes.

Original Array of Hashes

$VAR1 = [
      {
        'Color' => 'green',
        '2nd Color' => 'blue',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'ford',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:49:19'
      },
      {
        'Color' => 'red',
        '2nd Color' => 'green',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'ford',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:46:17'
      },
      {
        'Color' => 'red',
        '2nd Color' => 'blue',
        '3rd Color' => 'green',
        'toy' => 'truck',
        'toy_type' => 'chevy',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:52:14'
      },
      {
        'Color' => 'red',
        '2nd Color' => 'blue',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'chevy',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:24:14'
      },
      {
        'Color' => 'white',
        '2nd Color' => 'blue',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'gmc',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 06:24:14'
      },

New Array of Hashes I want saved to a variable:

$VAR2 = [
      {
        'Color' => 'green',
        '2nd Color' => 'blue',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'ford',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:49:19'
      },
      {
        'Color' => 'red',
        '2nd Color' => 'blue',
        '3rd Color' => 'green',
        'toy' => 'truck',
        'toy_type' => 'chevy',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 01:52:14'
      },
      {
        'Color' => 'white',
        '2nd Color' => 'blue',
        '3rd Color' => 'yellow',
        'toy' => 'truck',
        'toy_type' => 'gmc',
        'kind' => '4door',
        'Stage' => 'Production',
        'Step' => 'Platform',
        'Datetime' => '2012/06/08 06:24:14'
      },

Notice how I only wanted the most recent ford and recent chevy to be stored, but because there was only one gmc, I also wanted that stored.

I was referring to the perldsc (http://perldoc.perl.org/perldsc.html) documentation, but it did not go into this great of detail. Is this even possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:16:44+00:00Added an answer on June 5, 2026 at 12:16 pm
    sub key { join ':', @{ $_[0] }{qw( toy kind Stage Step )} }
    
    # Determine which records to keep.
    my %latest;
    for my $rec (@$recs) {
        my $key = key($rec);
        $latest{$key} = $rec->{Datetime}
           if !$latest{$key} || $latest{$key} lt $rec->{Datetime};
    }        
    
    # Filter out the others.
    @$recs = grep { $latest{key($_)}{Datetime} eq $_->{Datetime} } @$recs;
    

    The above method preserves the original order. It also handles ties elegantly (keeps both).

    If you don’t need to preserve the original order, you can use something simpler. Unfortunately, it only keeps one record in the event of a tie, and its performance doesn’t scale as well [O(N log N) instead of O(N)].

    sub key { join ':', @{ $_[0] }{qw( toy kind Stage Step )} }
    
    my %seen;
    @$recs =
       grep !$seen{key($_)}++,
        sort { $b->{Datetime} cmp $a->{Datetime} }
         @$recs;
    

    (Add a reverse in front of grep if you want the final result sorted by ascending Datetime.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I eliminate duplicate in an array using PHP, array looks like below
How to display only array value in JSON out in php I am using
I am using the method below to get an array from my plist and
My code is below. I have an array, and by using this array I
When a form gets posted, I get some checkbox values like the below: Array
I am using below code to send a byte array to a site. Why
I'm trying to initialize an array of structures to all-0's, using the below syntax:
I'm using Peter Below's PBThreadedSplashForm to display during application startup. It gets updated as
I am using below code to check ary_navigationControllerViews array contains myclass object or not
How to sort the array alphabetically below using as the key criterion label ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.