I suppose if your files are really huge (hundreds of MB), the slurping and splitting might be impractical. But these days, anything up to a 100 MB or so should fit comfortably.#!/usr/bin/perl use strict; die "Usage: $0 filename.csv\n" unless ( @ARGV and -f $ARGV[0] ); for my $csvname ( @ARGV ) { my $records = read_csv( $csvname ); if ( ref( $records ) ne 'ARRAY' ) { warn "Unable to pull records from file $csvname\n"; next; } elsif ( @$records == 0 ) { warn "No csv data found in file $csvname\n"; next; } do_something( $records ); } sub read_csv { my $filename = shift; open( IN, "<", $filename ) or do { warn "open failed on $filename: $!\n"; return; }; local $/; my $alldata = <IN>; my @records = grep !/^#|^\s*$/, split( /[\r\n]+/, $alldata ); return \@records; } sub do_something { # because just being able to read is seldom enough... }
(Updated to fix grammar in the opening sentence. I'd also suggest that "read_csv" should really be called something else, like "read_file_data" -- there's nothing particularly "csv-ish" about that sub.)
In reply to Re: Parsing a text file
by graff
in thread Parsing a text file
by calmthestorm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |