pbassnote has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: looping through a csv file
by kennethk (Abbot) on Feb 03, 2014 at 20:51 UTC | |
by pbassnote (Acolyte) on Feb 06, 2014 at 16:31 UTC | |
|
Re: looping through a csv file
by Kenosis (Priest) on Feb 03, 2014 at 20:51 UTC | |
|
Re: looping through a csv file
by Laurent_R (Canon) on Feb 03, 2014 at 21:32 UTC |