A list of lists type structure would seem easiest for what you want do (like what you've done), although it would seem easier to create a hash keyed on a primary identifier (1401 in the example maybe). This would also let you give elements English names, which makes coming back to the file format later a lot easier. So maybe something like:
#!C:\Perl\bin\perl use strict; use warnings; open my $input, "<", "input.csv" or die "Can not open: $!\n"; open my $output, ">", "output.csv" or die "Can not open: $!\n"; my %data = (); while(<$input>){ my @line = split /,/; my $key = shift @line; $data{$key} = {}; # Empty hash ref $data{$key}{store} = shift @line; $data{$key}{period} = shift @line; $data{$key}{dist} = \@line; # First three entries have been removed } foreach my $key (keys %data) { foreach my $dist ( @{ $data{$key}{dist} } ) { print $output join(",", $key, $data{$key}{store}, $data{$key}{peri +od}, $dist), "\n"; } } close $input; close $output;
Please also take note that I have changed your open statements to 3-argument opens, swapped to lexical file handles and changed from the high precedence || to the low precedence or. The virtues of these structures have been extolled extensively in the archives.
In reply to Re: Generating an output file
by kennethk
in thread Generating an output file
by TStanley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |