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.


In reply to Re: Generating an output file by kennethk
in thread Generating an output file by TStanley

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.