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 4565864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:42:18+00:00 2026-05-21T18:42:18+00:00

I have many, many lines of text data representing events occurring on various dates.

  • 0

I have many, many lines of text data representing events occurring on various dates. Each date is associated with ~500 or so events. Each event needs to be evaluated within the context of the other events occurring on that date, and only those other events. Since slurping all the data into an array and breaking it into smaller arrays is not feasible, memory-wise, I would like to use the recommended while loop procedure.

What I would like to do is the following: 1) pack an array with each line until the next line shows a different date; 2) process the array and clear it; 3) continue on packing the lines until you get to the next date, and so on.

So far, I’m using the following code, but it just seems to repeat too many things to be the most idiomatic solution:

my @chunk;
my $current;

while ( <FILEHANDLE> ) {
    my $date_of_this_line = ( split /\t/ )[0];
    unless ( defined $current and $current eq $date_of_this_line ) {
        do { &process @chunk; undef @chunk } if @chunk;
        $current = $date_of_this_line;
    }
    push @chunk, $_;
}
do{ &process @chunk; undef @chunk } if @chunk;

Any ideas for a little bit better solution to this kind of thing? I ask because I’m sure I’m not the first one to do this!

EDIT
I think I’ve got it! With the help of ysth’s and FM’s comments (below), I was able to reduce the solution to a few fewer lines of code without the repeating commands. The tradeoff is that I have to declare one more lexical variable before entering the while loop.

my @chunk;
my $current = 1;
my $date_of_line = 1;

while ( $date_of_line or @chunk ) {
    $date_of_line = defined( $_ = $FILEHANDLE ) ? ( split /\t/ )[0] : 0 and chomp;
    #the reason for 'and chomp'? chomp throws an error if $_ = $FILEHANDLE is not defined

    unless ( $current eq $date_of_line ) {
        process( splice( @chunk ) ) if @chunk;
        #thanks to ysth for pointing out how to process and clear @chunk in one stroke!
        $current = $date_of_line;
    }

    push @chunk, $_ if $date_of_line;
}

Not bad, eh? If I define the subroutine ‘process’ to give me a convenient little test, it confirms the results are what we want (that is… until I add more data and it screws up on me ;):

sub process {
    my @batch = @_;
    my $size = @batch;
    print "size is $size\n"; #simply tells me I'm getting the right size chunks;

    my $dates = keys %{ { map { ( split /\t/ )[0] => undef ) @batch } };
    print "number of different dates in batch: $games\n"; #should only be 1
}
  • 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-05-21T18:42:19+00:00Added an answer on May 21, 2026 at 6:42 pm

    Nope, that’s generally how to do it. You can pass and clear the array at once with &process( splice(@chunk) ) though. There’s a variation possible where you loop:

    while( ! $eof || @chunk ) {
        $eof ||= defined( $_ = <FILEHANDLE> );
        if ( $eof || defined($current) && $current ne ( $date_of_this_line = ( split /\t/ )[0] ) ) {
            &process( splice(@chunk) ) if @chunk;
            $current = $date_of_this_line;
        }
        push @chunk, $_ unless $eof;
    }
    

    but that’s kinda messy.

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

Sidebar

Related Questions

I have a test file that has many lines, each line looks something like:
I have a file with many lines in it. One of them look like
I have a file that is similar to this: <many lines of stuff> SUMMARY:
I have many forms that use AJAX (w/ jQuery) for validation and data submission.
I've got a text file that arrives at my application as many lines of
I have a program which reads data from 2 text files and then save
I have many text files of this format .... <snip> 'FOP' 0.19 1 24
I have to read in some data from text files formated like the example
What if you have a big text file that has many entries in it
I have this text file with customer, account and transaction information in. Each item

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.