in reply to Another flatfile thingy
OK, let me put my wallet where my mouth is and show you a hasty rewrite that takes all of that into account:
Cheers,=pod =item open_flatfile Takes the name of a pseudo-CSV flatfile and an optional delimiter as a +rgs. (The delimiter can be a regular expression created with qr//.) It ope +ns the file, uses the first line a a header, and returns the data as an array of hashes. This will not handle CSV files with escaped fields. =cut sub open_flatfile { my $file = shift; my $delim = shift || "\t"; local *FH; open (FH, "<$file") or die "Cannot read $file: $!"; my @contents = <FH>; close (FH); s/\r?\n$// foreach @contents; my @header = split ($delim, (shift @contents), -1); # Create an anonymous sub to do the work my $extract_row = sub { my @cols = split($delim, shift(@_), -1); my %row = map {($header[$_], $cols[$_])} 0..$#header; return \%row; }; return map {$extract_row->($_)} @contents; }
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: A few style suggestions
by chip (Curate) on Aug 11, 2000 at 05:53 UTC | |
by tilly (Archbishop) on Aug 11, 2000 at 15:23 UTC | |
(bbq) RE: A few style suggestions
by BBQ (Curate) on Aug 04, 2000 at 20:57 UTC |