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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:11:58+00:00 2026-05-31T10:11:58+00:00

I think I’m probably missing something obvious here, so please enlighten me. Currently I’m

  • 0

I think I’m probably missing something obvious here, so please enlighten me.

Currently I’m reading a CSV file into perl using Text::CSV and it’s ‘parse’ method (outlined below).

csv->parse method:

while (<FILE>) {
  if ($csv->parse($_)) {
     my @columns = $csv->fields();
     'refer to items with: $columns[1]'
  }
  else {
     'Handle the parse error here'
  }
}

I’m now looking for a way to read these values into a hash instead of an array. Going through the Text::CSV documentation it seems the most efficient way to do this is by using the ‘getline’ method (Outlined below), but I’m unsure how to catch errors in a similar manner to the way they are caught using the array approach.

csv->getline method:

my @cols = ("col1", "col2", "col3");
my $item = {};
$csv->bind_columns( \@{$item}{@cols} );
while( $csv->getline($it_fh) ) {
  'refer to items using: $item->{col1}'
}

Any hints/tips/links would be great, as my Googleing seems to have come up empty?

EDIT: So here’s my understanding of the answer I’ve accepted, just to clarify what I understand as the fault tolerance of this method.

$csv->column_names( qw(col1 col2 col3) );
my $line;
until ( eof(FILE) ) {
    $line++;
    my $item = $csv->getline_hr( \*FILE );
    if ( $item ) {
        # refer to items as $item->{col1}
    } else {
        my $err = "Line: " . $line . "failed to parse\n"
        . "Input: " . $csv->error_input . "\n"
        . "Error: " . $csv->error_diag . "\n";
        print STDERR $err;
    }
}
  • 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-31T10:12:00+00:00Added an answer on May 31, 2026 at 10:12 am

    Well, there’s always the straightforward approach:

    my @cols = qw(col1 col2 col3);
    
    while ( <FILE> ) {
       if ( $csv->parse($_) ) {
           my %item;
           @item{@cols} = $csv->fields();
           # refer to items using $item{col1}
       }
       else {
           # handle the parse error here
       }
    }
    

    However, I suspect that the following may be a bit more efficient, at least if using the XS implementation of Text::CSV:

    $csv->column_names( qw(col1 col2 col3) );
    
    until ( eof(FILE) ) {
        my $item = $csv->getline_hr( \*FILE );
        if ( $item ) {
            # refer to items as $item->{col1}
        } else {
            # handle the parse error here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm missing something basic here. Why is the third IF condition true?
I think I'm missing something basic here. Any explanation or pointers to previously asked
I think this has probably been asked, but after reading a lot, I'm not
I think I got all the steps down but I must be missing something
I think I am missing something about the sphinx extension for doctest. The typical
I think the file that is produced is an .asm file, any idea how
I think most people here understand the importance of fully automated builds. The problem
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
Think about a rectangular div, which is split into 2 parts, each part is

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.