Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get the error message »Failed to parse line« with Text::CSV?
    primarykey
    data
    text
    <p>I'm trying to parse a CSV file in Perl, but don't really understand examples I found on the Internet. Could someone explain me this example?</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $file = 'dhcp.csv'; my $csv = Text::CSV-&gt;new(); open (CSV, "&lt;", $file) or die $!; while (&lt;CSV&gt;) { next if ($. == 1); if ($csv-&gt;parse($_)) { my @columns = $csv-&gt;fields(); print "Name: $columns[0]\n\tContact: $columns[4]\n"; } else { my $err = $csv-&gt;error_input; print "Failed to parse line: $err"; } } close CSV; </code></pre> <p>When I run it, I get <code>Failed to parse line</code>. What does the <code>$.</code> stand for? And <code>$_</code>?</p> <p>My goal is to find the line where there is the computer name I search for. After that, I can find the corresponding MAC address. I hope this is comprehensible, thanks.</p> <hr> <p><em>EDIT</em>:</p> <p>My CSV file looks like:</p> <pre><code>172.30.72.22,DEC-16.rec.local,001676b755d6,Bart SIMPSONS,Ordinateur de bureau,DEC/DECVA,002,SR2 0.12,,Accès complet,N/D,Aucun 172.30.72.23,DEC-20.rec.local,001688b7bfdc,Larry Wall,Ordinateur de bureau,DEC/DECVA,003,?,,Accès complet,N/D,Aucun </code></pre> <p>Field #2 is the hostname, I want to resolve field #3 (MAC address) by field #2.</p> <hr> <p><em>EDIT n°2</em>:</p> <p>In fact, don't need to parse the CSV file for my purpose. I found a bash solution, fast enough for my application.</p> <pre><code>my $macAdd = `cat dhcp.csv | grep {computerName} | cut -d ',' -f 5` </code></pre> <p>Done !<br> Thanks for your help, one day I'll have to parse a csv file, sure.</p> <p>3rd edit : don't know who edited my post and the topic question, but that's not it at all !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload