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

  • Home
  • SEARCH
  • 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 8079779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:08+00:00 2026-06-05T16:13:08+00:00

I have array INPUTFILES with n files INPUTFILES=( file_0 … files_n-1 ) And i

  • 0

I have array INPUTFILES with n files

INPUTFILES=( file_0 ... files_n-1 )

And i need to sort them in the array order by first row in files.

Files look like this:

2012.09.20 17:10
2012.11.21 00:10
2012.12.22 15:10
2012.12.23 15:10

I have already function to compare 2 files:

IsSooner () {
ONEFIRST=$( head -1 "${1}" ) 
ONELAST=$( tail -1 "${1}" )
TWOFIRST=$( head -1 "${2}" ) 
TWOLAST=$( tail -1 "${2}" )

TIMEFORMAT='Y.%m.%d %H:%M:'

perl <<EOF
use strict;
use warnings;

use Time::Piece;

open STDERR, "> /dev/null";

my @dates1 = ("${ONEFIRST}","${ONELAST}");
my @range1 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates1;

my @dates2 = ("${TWOFIRST}","${TWOLAST}");
my @range2 = map Time::Piece->strptime("\$_", "${TIMEFORMAT}"), @dates2;

if ( \$range1[0] < \$range2[0] ) {
  exit 0;
}

exit 1;
EOF

[ $? -eq 0 ] && {
  return 0
}

return 1  
}

Earlier will be first date in the file, the smaller index in the array will have.

Solution in BASH if preferable.

UPDATE
I don’t know format of date in advance. I just know it will be in strftime(3c) format.

  • 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-05T16:13:10+00:00Added an answer on June 5, 2026 at 4:13 pm
    1. In simple loop read first line of each file and save this information to hash, having first row data a hash key and the file name a hash value.

      my @inpufiles = ...;
      my %hash;
      foreach (@inputfiles) {
        open(my $fh, $_) or die $!;
        $hash{<$fh>} = $_;
        close $fh;
      }
      
    2. Sort the hash by the keys and print all values of sorted hash.

      foreach (sort (keys(%hash))) {
        print "$hash{$_}\n";
      }
      

      If you don’t want to print it, just store it back to the array, then do just

      @inputfiles = map {$hash{$_}} sort (keys(%hash));
      

    Good luck!


    [UPDATE]

    To follow the update in your question I suggest you to store values to hash using:

    $hash{Time::Piece->strptime(<$fh>, $timeformat)->epoch} = $_;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array that need to sort by their appearance, as they are written
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
An array is defined of assumed elements like I have array like String[] strArray
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
Have an array of chars like char members[255]. How can I empty it completely
I have array like below, Array ( [14289] => Array ( [0] => Karthikeyan
i have array like below which is sorted by array_count_values function, Array ( [Session
I have array [Company] => Demo Company 1 [First Name] => Test [Last Name]
I have array like this: array( 'person0' => array( 'name'=>'name0','address'=>'address0' ), 'person1' => array(

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.