On 8/6/06, Ralph H. Stoos Jr. <[EMAIL PROTECTED]> wrote:
All,

I am a major newbie (and 53 years old to boot), but I was able to piece
together a workable script that processes a CSV file.

I get a CSV file once a week that has data which needs to be sorted.

For the first pass, I have the script gives me an output file that
contains records that meet one parameter (in this case it is a Pages Per
Minute (PPM).  I did the minimal smart thing to have the input file
chosen by the user and the output file as well (not hard-coded to a
specific name).

Next, what I want to do is to further parse the first output file for
other parameters like paper size.

The way it works is that the script will ask the user what to include
and that would start at all records that are for 120 PPM. Then I want to
make another file from the 120 PPM file that would be all records that
are 120 PPM and have a specific paper size (I get the paper size in
microns but do the divide by 25400 real quick to get inch dimensions).

At the end of all this parsing which will result in about 20 separate
CSV files, I need to do do some other math based on what portions of the
records contain.

Ralph,

What you're doing here isn't that complicated. It shouldn't require
multiple passes through the file, and it shouldn't require too many
subroutines, either, since it doesn't sound like you're really going
going to look for more than a couple of different things. Writing a
general purpose subroutine to parse the fileds seems like a bit of
overkill in this situation, you can probably get away with some nested
foreach loops. If you find yourself getting too many levels deep with
the loops, though, it'll be time to move some things off into
subroutines.

Do you know the Text::CSV module? If not, it's time to look it up; It
should help do most of what you want to do. also, I question why
you're breaking this out into so many files, but that's up to you.
given multiple output files, I'd do something like this (I've included
some dummy data so you can see how it works):

#!/usr/bin/perl
use warnings;
use strict;

use Text::CSV;

my $file = shift;
my $outfile = shift;
my @papers = qw/ a4 a6 a7 a8 a12 /;
my @ppms = qw/ 120 240 360 244 /;


my $csv = Text::CSV->new;
my @data;

# open FILE, "<", $file or die "$!\n";

my $header = <DATA>;

while (<DATA>) { # you'll use FILE, but we'll use DATA
   chomp;
   next unless $csv->parse($_);
   push @data, [$csv->fields];
}

foreach my $ppm (@ppms) {
   my @ppm = grep { $_->[1] == $ppm } @data;
   foreach my $paper (@papers) {
       open(OUT, ">", $outfile . $ppm . $paper) or die "$!\n";
       foreach (grep { $_->[2] eq $paper } @ppm){
           print OUT $csv->string(), "\n" if $csv->combine();
       }
       close OUT;
   }
}

__END__
ROOM,PPM,TS,PROJ,TV,VCR,DVD,COMP,LPTP,SCR,OVH,DC,SB,WKST
305,120,a4,X,,X,X,X,X,X,X,,,X
307,120,a6,X,,X,X,X,X,X,X,,,X
309,120,a4,X,,X,X,X,X,X,X,,,X
310,240,a7,X,,X,,X,X,X,X,,,
311,240,a12,X,,X,X,X,X,X,X,,,X
404,244,a4,,X,X,X,,,X,X,,,
406,120,a4,,X,X,X,,,X,X,,,
413,240,a8,X,,X,X,X,X,X,X,,,
413A,360,a8,,X,,X,X,X,X,X,X,,,
417,360,a4,X,,X,X,X,X,X,X,,,
419,360,a12,X,,X,X,X,X,X,X,,,
424,360,a4,X,,X,X,X,X,X,X,X,,
502,360,a9,X,,X,X,X,X,X,X,,,
504,244,a8,X,,X,X,X,X,X,X,,,
506,244,a7,X,,X,X,X,X,X,X,,,
508,120,a7,,X,X,X,,,X,X,,,


HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to