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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:38:28+00:00 2026-05-22T14:38:28+00:00

I am trying to process a text file in perl. I need to store

  • 0

I am trying to process a text file in perl. I need to store the data from the file into a database.
The problem that I’m having is that some fields contain a newline, which throws me off a bit.
What would be the best way to contain these fields?

Example data.txt file:

ID|Title|Description|Date
1|Example 1|Example Description|10/11/2011
2|Example 2|A long example description
Which contains
a bunch of newlines|10/12/2011
3|Example 3|Short description|10/13/2011

The current (broken) Perl script (example):

#!/usr/bin/perl -w
use strict;

open (MYFILE, 'data.txt');
while (<MYFILE>) {
    chomp;
    my ($id, $title, $description, $date) = split(/\|/);

    if ($id ne 'ID') {
        # processing certain fields (...)

        # insert into the database (example)
        $sqlInsert->execute($id, $title, $description, $date);
    }
}
close (MYFILE);

As you can see from the example, in the case of ID 2, it’s broken into several lines causing errors when attempting to reference those undefined variables. How would you group them into the correct field?

Thanks in advance! (I hope the question was clear enough, difficult to define the title)

  • 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-22T14:38:29+00:00Added an answer on May 22, 2026 at 2:38 pm

    I would just count the number of separators before splitting the line. If you don’t have enough, read the next line and append it. The tr operator is an efficient way to count characters.

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    
    open (MYFILE, '<', 'data.txt');
    while (<MYFILE>) {
        # Continue reading while line incomplete:
        while (tr/|// < 3) {
            my $next = <MYFILE>;
            die "Incomplete line at end" unless defined $next;
            $_ .= $next;
        }
    
        # Remaining code unchanged:
        chomp;
        my ($id, $title, $description, $date) = split(/\|/);
    
        if ($id ne 'ID') {
            # processing certain fields (...)
    
            # insert into the database (example)
            $sqlInsert->execute($id, $title, $description, $date);
        }
    }
    close (MYFILE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to import some sales data into my MySQL database. The data
I'm trying to debug a problem with a perl application that calls some c
I am trying to process an uploaded file in a Perl program, using CGI::Application.
I'm trying to process data obtained from a run of diff to an instance
I need to know when a text file that's opened by C# is not
I am trying to process a CSV file in which some of the fields
Im trying to read a text file, x bytes at a time into a
I am currently trying to import a list of executables from a text file
I'm trying to process a large text file through a HttpServlet (tomcat). As this
I am trying to read in a large text file (~2.5 MB) into my

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.