Re: Splitting a CSV file at a variable number



On 6/28/07, sum_duud <Turnbull.Glenn@xxxxxxxxx> wrote:
in essence I would like the perl script to output all the fourth
column "0" values to a file called phase_0.csv and all the "1" values
to phase_1.csv etc.
snip

use an array of file handles (warning, untested):

use strict;
use warnings;

open my $in, '<', "phase.csv" or die "Could not open phase.csv: $!\n";
<$in>; #discard first line

my @file = map {
open my $fh, '>', "phase_$_.csv"
or die "could not open phase_$_.csv: $!";
$fh
} 0 .. 20;

while (<$in>) {
chomp;
my ($x, $y, $z, $phase) =split /\s*,\s*/;
my $file = $file[$phase];
print $file "$x, $y, $z, $phase\n";
}
.



Relevant Pages

  • Re: Destroyed Highways
    ... because that highway had been destroyed in a hurricane. ... I've seen pictures of roads in Hawaii being ... For the past few years Rand McNally atlases have included a warning ... tag on the Mississippi map warning about U.S. 90's crossings over St ...
    (misc.transport.road)
  • Newbie mapping trouble (cXML OrderRequest to Generic PO)...compiler links
    ... I'm trying to create a map between a cXML OrderRequest ... Warning 2 The destination node "ShipToID" is required but the source node ... Warning 3 The destination node "LineNumber" is required but the source node ... to the elements in the destination schema. ...
    (microsoft.public.biztalk.general)
  • Re: Cant get desired o/p with this code
    ... use strict; ... use warning; ... command line I am not getting any error message on screen. ... In the case of version checking I am not getting any o/p ...
    (perl.beginners)
  • Re: Uncheck Warning
    ... assign the Map to a temporary variable so that you can narrow the @SuppressWarningsdown to the minimum. ... The warning is because you can't do generic casts without a warning. ... Map bar = // I took the liberty of widening the target type ... session.setAttribute("myAttributes", mySessionAttribute); ...
    (comp.lang.java.programmer)
  • Re: A Snowboarder Does Something Stupid
    ... Jeff wrote: ... by your own description and looking at the map, I think you had time to ... and there were probably warning sings to say to slow down ... feel I could stop without buying one in the back. ...
    (rec.skiing.alpine)