What are you doing with these files? Is there a pre-existing script/program that you will feed them to, or are you just using this for temporary storage. If the latter, you may consider using the
Storable module instead of creating some custom format. As well, you may want to use some of the existing Perl CSV modules (like
Text::CSV) rather than rolling your own.
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.