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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:05:01+00:00 2026-06-08T22:05:01+00:00

I want each (small) file specified with ARGV read in its own array. If

  • 0

I want each (small) file specified with ARGV read in its own array. If I don’t test $ARGV, <> will slurp all files in a single table. Is there a better/shorter/simpler way of doing it?

# invocation: ./prog.pl *.txt

@table = ();
$current = "";
while (<>)
{
  if ($ARGV ne $current)
  {
    @ar = ();
    $current = $ARGV;
    if ($current)
    {
      push @table, \@ar;
    }
  }
  push @ar;
}
  • 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-08T22:05:03+00:00Added an answer on June 8, 2026 at 10:05 pm

    Please always use strict and use warnings at the top of your programs, and declare variables close to their first point of use using my.

    It is simplest to test end of file on the ARGV filehandle to determine when a new file is about to be opened.

    This code uses a state variable $eof to record whether the previous file has been completely read to avoid unnecessarily adding a new element to the @table array when the end of the @ARGV list is reached.

    use strict;
    use warnings;
    
    my @table;
    my $eof = 1;
    
    while (<>) {
      chomp;
      push @table, [] if $eof;
      push @{$table[-1]}, $_;
      $eof = eof;
    }
    

    @Alan Haggai Alavi’s idea of incrementing an index at end of file instead of setting a flag is far better as it avoids the need to explicitly create an empty array at the start of each file.

    Here is my take on his solution, but it is completely dependent on Alan’s post and he should gete the credit for it.

    use strict;
    use warnings;
    
    my @table;
    my $index = 0;
    
    while (<>) {
      chomp;
      push @{$table[$index]}, $_;
      $index++ if eof;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small program that will read a plain ASCII text file
I have several small classes that are all peers of each other declared and
i want to read\write a binary file which has the following structure: The file
for performance testing purposes I want to take a small XML file and create
I've created a small script to test out getting the basename of all files
I am trying to write a small java program that will accept a file
I want each action taken on certain tables to be logged. I want logging
Im using jquery and want each form text input to appear one by one,
I have a DevExpress GridControl and i want each cell to be multiline. I
I have a list view with 10 columns and I want each row to

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.