Rather than trying to calculate a negative offset equating to the first record - and writing a multi-line comment explaining what you're doing - you can just reposition the file pointer to the start of the file with seek.

In the code below, $called_first_row represents what you called $first_row. Perhaps $first_wanted_row, or something similar, would have been a better choice of variable name; $fourth_row would have been completely accurate.

#!/usr/bin/env perl use 5.010; use strict; use warnings; use Text::CSV_XS; my $csv_file = q{pm_csv_first_cell.csv}; open my $csv_fh, q{<}, $csv_file or die $!; my $csv_obj = Text::CSV_XS::->new(); my $called_first_row = $csv_obj->getline_all($csv_fh, 3, 1); say qq{@{$called_first_row->[0]}}; seek $csv_fh, 0, 0; my $real_first_row = $csv_obj->getline_all($csv_fh, 0, 1); say qq{@{$real_first_row->[0]}}; say q{First cell value: }, $real_first_row->[0][0]; close $csv_file;

Sample run and file contents:

ken@ganymede: ~/tmp $ cat pm_csv_first_cell.csv z,x,c,v,b,n a,s,d,f,g,h q,w,e,r,t,y 1,2,3,4,5,6 ken@ganymede: ~/tmp $ pm_csv_first_cell.pl 1 2 3 4 5 6 z x c v b n First cell value: z ken@ganymede: ~/tmp $

-- Ken


In reply to Re^3: Parse .csv file by kcott
in thread Parse .csv file by Sherlock Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.