First, the standard disclaimer: I am new to programmming, and even newer to Perl programmming. I have a large csv text file that I'll call folks.txt in which each line of the file is organized into 5 columns of data in which the first column is a number, and the next four columns are e-mail addrs. The first thing I do with folks.txt is:

open(FOLKS, "folks.txt") || die "can't open folks: $!"; while (<FOLKS>) { chomp; ($number, $addr1, $addr2, $addr3, $addr4) = (split /,/)[0,1,2,3,4];

What I wish to do with this data is to: 1) add four labels to associate with each of the four addrs, then 2) output each line formatted so that each corresponding label-addr pair is printed out with its corresponding number. So, if I were to define each of the labels like so:

$label1 = "fee" $label2 = "fi" $label3 = "fo" $label4 = "fum"

somewhere, likely above my "open(...)" line, that should work. But here I finally get to the problem. I want to be able to iterate through folks.txt four complete times in order to get the four labels associated with each of the four addrs. If I try to do this:

print "$label1,$number,$addr1\n"; print "$label2,$number,$addr2\n"; print "$label3,$number,$addr3\n"; print "$label4,$number,$addr4\n";

It produces a csv file, but not the way I need, as this will loop through the four print statements in the consecutive order just as shown above. What I need is for the first print statement to iterate through the entire folks.txt file, then the second print statement to iterate through the entire folks.txt file, and so on through the other two print statements. I believe that this can be accomplished with a looping construct, but since I'm kind of new to programming, I don't have a good command of loops. Suggestions, please? TIA, Dave


In reply to looping through a csv file by pbassnote

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.