Welcome to the monastery.

First, rather that iterating over the file 4 times (possible, but inefficient -- see seek), I would suggest pulling in all the data in one loop, and then processing it in a second loop. My experience has always been the first step to understanding how to deal with data is knowing what kind of file structure to use. You could store the lines of the file in an array, or you could use an array of arrays and then traverse that data structure later. Since you have your labels, maybe something like:

use strict; use warnings; my @labels = ('fee', 'fi', 'fo', 'fum'); open(my $fh, '<', "folks.txt") || die "can't open folks: $!"; my @list; while (<$fh>) { chomp; push @list, [split /,/]; } for my $element (@list) { print "$labels[0],$element->[0],$element->[1]\n"; } for my $element (@list) { print "$labels[1],$element->[0],$element->[2]\n"; } for my $element (@list) { print "$labels[2],$element->[0],$element->[3]\n"; } for my $element (@list) { print "$labels[3],$element->[0],$element->[4]\n"; }
or, a little better:
use strict; use warnings; my @labels = ('fee', 'fi', 'fo', 'fum'); open(my $fh, '<', "folks.txt") || die "can't open folks: $!"; my @list; while (<$fh>) { chomp; push @list, [split /,/]; } for my $i (0 .. 3) { for my $element (@list) { print "$labels[$i],$element->[0],$element->[$i+1]\n"; } }
or maybe even
use strict; use warnings; my @labels = ('fee', 'fi', 'fo', 'fum'); open(my $fh, '<', "folks.txt") || die "can't open folks: $!"; my @list; while (<$fh>) { chomp; my @line = split /,/; my %hash = (number => shift @line); for my $i (0 .. $#labels) { $hash{$labels[$i]} = $line[$i]; } push @list, \%hash; } for my $label (@labels) { for my $element (@list) { print "$label,$element->{number},$element->{$label}\n" } }

Second, please note I added some use statements at the top of the script. Read Use strict warnings and diagnostics or die to learn why. I also swapped to a 3-argument open with an indirect file handle. Especially if you are just learning, good habits to develop.

Third, rather than rolling you own, try using CPAN. In particular, for dealing with CSV, try Text::CSV. Did you know CSV files contain escaping sometimes? Text::CSV does, and it's tested.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: looping through a csv file by kennethk
in thread 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.