> Hello Rich

Hi, Chris, thanks for your response


> 
> See docs for perlfunc, specifically split.
> 
> Especially, the form for split
>     'split /PATTERN/,EXPR,LIMIT'
> 
> By setting the limit, you will be able to solve the problem.
> 
> Chris
> 

The thing is I can't be sure that there will never be embedded commas in
the data (well maybe I can with this particular set of data, not sure),
so split (as discussed in the Cookbook) isn't the way to go. This is why
I was looking to use a specialized module for the job. Also, I need to
be able to address several of the fields specifically, to change their
values, so I like the idea of using a hash which is done for me with
something like Tie::Handle::CSV.

What I've come up with is this:

<code>
#!/usr/bin/perl -w
use strict;
use Tie::Handle::CSV;

my $file = shift or die "You forgot to provide the name of a CSV
file\n";
my $csv_fh = Tie::Handle::CSV->new(file => $file, header => 0);

my %csv_headers = (
   0 => 'Num',
   1 => 'Env Num',
   2 => 'Envelope', 
   3 => 'Transaction',
   4 => 'Lockbox',
   5 => 'Date',
   6 => 'Time', 
   7 => 'Batch',
   8 => 'Batch Item',
   9 => 'Check',
   10 => 'Check Amount', 
   11 => 'ABA/RT',
   12 => 'Account Num',
   13 => 'Check Num',
   14 => 'Check Image', 
   15 => 'Envelope Image',
   16 => 'Page Images'  
);   


my %csv_hash;
while (my $csv_line = <$csv_fh>) {
   next if ($. < 4 );

   for my $index (0..15) {
      $csv_hash{ $csv_headers{$index} } = $csv_line -> [$index];
   }

   # The values that remain in @{$csv_line} are all associated with
"Page Images", so
   # we need to build an array to pass as a hash value...
   $csv_hash{'Page Images'} = [];
   for my $index (16..$#{$csv_line}) {
      push @{ $csv_hash{'Page Images'} }, $csv_line -> [$index];
   }

   for (sort keys %csv_hash) {
      if ( !ref $csv_hash{$_}  ) { # If the value is not a (array) ref
         print $_, " => ", $csv_hash{$_}, "\n";
      } else {
         print $_, " => \n";

         for ( @{ $csv_hash{$_} } ) {
            print "\n\t", $_;
         }
         print "\n\n";
      }
   }
   print "\n\n\n";
   print '*' x 20, "\n";
}

</code>

Which does what I want, but it seems like there should be an easier
way...

richf


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to