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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:24:49+00:00 2026-05-19T22:24:49+00:00

I have a lot of text files with fixed-width fields: <c> <c> <c> Dave

  • 0

I have a lot of text files with fixed-width fields:

<c>     <c>       <c>
Dave    Thomas    123 Main
Dan     Anderson  456 Center
Wilma   Rainbow   789 Street

The rest of the files are in a similar format, where the <c> will mark the beginning of a column, but they have various (unknown) column & space widths. What’s the best way to parse these files?

I tried using Text::CSV, but since there’s no delimiter it’s hard to get a consistent result (unless I’m using the module wrong):

my $csv = Text::CSV->new();
$csv->sep_char (' ');

while (<FILE>){
    if ($csv->parse($_)) {
        my @columns=$csv->fields();
        print $columns[1] . "\n";
    }
}
  • 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-19T22:24:50+00:00Added an answer on May 19, 2026 at 10:24 pm

    As user604939 mentions, unpack is the tool to use for fixed width fields. However, unpack needs to be passed a template to work with. Since you say your fields can change width, the solution is to build this template from the first line of your file:

    my @template = map {'A'.length}        # convert each to 'A##'
                   <DATA> =~ /(\S+\s*)/g;  # split first line into segments
    $template[-1] = 'A*';                  # set the last segment to be slurpy
    
    my $template = "@template";
    print "template: $template\n";
    
    my @data;
    while (<DATA>) {
        push @data, [unpack $template, $_]
    }
    
    use Data::Dumper;
    
    print Dumper \@data;
    
    __DATA__
    <c>     <c>       <c>
    Dave    Thomas    123 Main
    Dan     Anderson  456 Center
    Wilma   Rainbow   789 Street
    

    which prints:

    template: A8 A10 A*
    $VAR1 = [
              [
                'Dave',
                'Thomas',
                '123 Main'
              ],
              [
                'Dan',
                'Anderson',
                '456 Center'
              ],
              [
                'Wilma',
                'Rainbow',
                '789 Street'
              ]
            ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several entity classes that I use for parsing fixed width text files
So, I have created a big website (a lot of text in different files
I have a Java program which uses a lot of text files. The program
I have text files with a lot of uniform rows that I'd like to
I have a lot of files that have errors because the imported text uses
I have a lot of text data and want to translate it to different
I have a table with a lot of text inputs like these: alt text
I have a JLabel which has a lot of text on it. Is there
I have an text outline tree where i display a lot of information There
i have a text held in my main view and a button next 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.