Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Generating an output file

by kennethk (Abbot)
on Sep 22, 2009 at 15:23 UTC ( [id://796733]=note: print w/replies, xml ) Need Help??


in reply to Generating an output file

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.

Replies are listed 'Best First'.
Re^2: Generating an output file
by TStanley (Canon) on Sep 22, 2009 at 16:11 UTC
    This worked exactly as advertised. Thank you for your help.

    TStanley
    --------
    People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-18 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found