Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: looping through a csv file

by kennethk (Monsignor)
on Feb 03, 2014 at 20:51 UTC ( #1073264=note: print w/ replies, xml ) Need Help??


in reply to looping through a csv file

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.


Comment on Re: looping through a csv file
Select or Download Code
Re^2: looping through a csv file
by pbassnote (Novice) on Feb 06, 2014 at 16:31 UTC

    You guys are awesome. It'll take me some time to figure out your coding suggestions, especially the CPAN one, but it'll be time well spent. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1073264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-22 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (184 votes), past polls