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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:26:26+00:00 2026-06-01T00:26:26+00:00

I have a text file to parse in Perl. I parse it from the

  • 0

I have a text file to parse in Perl. I parse it from the start of file and get the data that is needed.

After all that is done I want to read the last line in the file with data. The problem is that the last two lines are blank. So how do I get the last line that holds any data?

  • 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-01T00:26:27+00:00Added an answer on June 1, 2026 at 12:26 am

    If the file is relatively short, just read on from where you finished getting the data, keeping the last non-blank line:

    use autodie ':io';
    open(my $fh, '<', 'file_to_read.txt');
    # get the data that is needed, then:
    my $last_non_blank_line;
    while (my $line = readline $fh) {
        # choose one of the following two lines, depending what you meant
        if ( $line =~ /\S/ ) { $last_non_blank_line = $line }  # line isn't all whitespace
        # if ( line !~ /^$/ ) { $last_non_blank_line = $line } # line has no characters before the newline
    }
    

    If the file is longer, or you may have passed the last non-blank line in your initial data gathering step, reopen it and read from the end:

    my $backwards = File::ReadBackwards->new( 'file_to_read.txt' );
    my $last_non_blank_line;
    do {
        $last_non_blank_line = $backwards->readline;
    } until ! defined $last_non_blank_line || $last_non_blank_line =~ /\S/;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that I parse each month and insert the data
I have to parse a tab-delimited text file with Ruby to extract some data
I have Text file that contains data separated with a comma , . How
Perl newbie here. I have a log file that I need to parse out
I have text file with several thousands lines. I want to parse this file
I have a text file with Tag - Value format data. I want to
I have an html page that has particular text that I want to parse
I have a text file in the following url. http://hosted.stats.com/ifb2009/data.asp?file=en/chlg_standings.txt I want to parse
I Have a text file and want to parse it with PHP any one
I want to parse a text file to get what I want and create

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.