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

When coding up Ajax calls in ASP.Net MVC we have a lot of options
I am having a lot of problems with Vimeo and IE. I have a
I have placed my static web application in an asset folder. My static web
I have a Flex application here If you open that link, you'll see it
I know this has probably been asked a lot on here, but I am
I realize this is not terribly different from a lot of other questions that
First of all, this only happens on IE, in firefox 3.6 everything works well,
All right guys and gals it's time for the age old question, how do
I am having some issues with the TRichEdit. The first issue is if I
The layout I try to implement is (I think) pretty simple. One TextView expands

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.