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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:35:00+00:00 2026-06-17T20:35:00+00:00

I’m grabbing a few columns from a tab delineated file in Perl. The first

  • 0

I’m grabbing a few columns from a tab delineated file in Perl. The first line of the file is completely different from the other lines, so I’d like to skip that line as fast and efficiently as possible.

This is what I have so far.

my $firstLine = 1;

while (<INFILE>){
    if($firstLine){
        $firstLine = 0;
    }
    else{
        my @columns = split (/\t+/);
        print OUTFILE "$columns[0]\t\t$columns[1]\t$columns[2]\t$columns[3]\t$columns[11]\t$columns[12]\t$columns[15]\t$columns[20]\t$columns[21]\n";
    }
}

Is there a better way to do this, perhaps without $firstLine? OR is there a way to start reading INFILE from line 2 directly?

Thanks in advance!

  • 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-17T20:35:01+00:00Added an answer on June 17, 2026 at 8:35 pm

    Let’s get some data on this. I benchmarked everybody’s techniques…

    #!/usr/bin/env perl
    
    sub flag_in_loop {
        my $file = shift;
    
        open my $fh, $file;
    
        my $first = 1;
        while(<$fh>) {
            if( $first ) {
                $first = 0;
            }
            else {
                my $line = $_;
            }
        }
    
        return;
    }
    
    sub strip_before_loop {
        my $file = shift;
    
        open my $fh, $file;
    
        my $header = <$fh>;
        while(<$fh>) {
            my $line = $_;
        }
    
        return;
    }
    
    sub line_number_in_loop {
        my $file = shift;
    
        open my $fh, $file;
    
        while(<$fh>) {
            next if $. < 2;
    
            my $line = $_;
        }
    
        return;
    }
    
    sub inc_in_loop {
        my $file = shift;
    
        open my $fh, $file;
    
        my $first;
        while(<$fh>) {
            $first++ or next;
    
            my $line = $_;
        }
    
        return;
    }
    
    sub slurp_to_array {
        my $file = shift;
    
        open my $fh, $file;
    
        my @array = <$fh>;
        shift @array;
    
        return;
    }
    
    
    my $Test_File = "/usr/share/dict/words";
    print `wc $Test_File`;
    
    use Benchmark;
    
    timethese shift || -10, {
        flag_in_loop        => sub { flag_in_loop($Test_File); },
        strip_before_loop   => sub { strip_before_loop($Test_File); },
        line_number_in_loop => sub { line_number_in_loop($Test_File); },
        inc_in_loop         => sub { inc_in_loop($Test_File); },
        slurp_to_array      => sub { slurp_to_array($Test_File); },
    };
    

    Since this is I/O which can be affected by forces beyond the ability of Benchmark.pm to adjust for, I ran them several times and checked I got the same results.

    /usr/share/dict/words is a 2.4 meg file with about 240k very short lines. Since we’re not processing the lines, line length shouldn’t matter.

    I only did a tiny amount of work in each routine to emphasize the difference between the techniques. I wanted to do some work so as to produce a realistic upper limit on how much performance you’re going to gain or lose by changing how you read files.

    I did this on a laptop with an SSD, but its still a laptop. As I/O speed increases, CPU time becomes more significant. Technique is even more important on a machine with fast I/O.

    Here’s how many times each routine read the file per second.

    slurp_to_array:       4.5/s
    line_number_in_loop: 13.0/s
    inc_in_loop:         15.5/s
    flag_in_loop:        15.8/s
    strip_before_loop:   19.9/s
    

    I’m shocked to find that my @array = <$fh> is slowest by a huge margin. I would have thought it would be the fastest given all the work is happening inside the perl interpreter. However, it’s the only one which allocates memory to hold all the lines and that probably accounts for the performance lag.

    Using $. is another surprise. Perhaps that’s the cost of accessing a magic global, or perhaps its doing a numeric comparison.

    And, as predicted by algorithmic analysis, putting the header check code outside the loop is the fastest. But not by much. Probably not enough to worry about if you’re using the next two fastest.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was

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.