#!/usr/local/bin/perl use strict; use warnings; use Text::CSV; use FileHandle; my $i=1; my @fields; my @array; my @list = (11, 15, 18); #my $colref=\@fields; my @headers; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); #my $IN = new FileHandle; my ($logfile, $logpath); $logpath = 'C:\\Users\\amwang\\Desktop'; $logfile = "$logpath\\log.txt"; open (LOG,">>","$logfile") || die ("Error : can't open log file"); my $file = "$logpath\\TCT_AGM.csv"; # foreach loop execution foreach $a (@list) { print "value of a: $a\n"; open(my $data, "<$file") or die "Could not open '$file' $!\n"; while (my $line = <$data>) { chomp $line; if ($csv->parse($line)) { my $field_count = $csv->fields(); while (my $fields = $csv->getline( $data )) { push(@headers,$fields->[$a]); } } else { warn "Line could not be parsed: $line\n"; } print LOG $headers [2], "\t"; } close ($data); }

I want to print different col value of my file, but only able to do the very first value in the foreach loop. i think the problem is i need to move to the first row of the array again at the end of while loop, not sure how can i do it..


In reply to how to move to the first row of array after passing it through a while loop by wangruonan

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.