############# sub build_hash ############# { my ( $file_, $numfields_ ) = @_; my $line = 0; my ( %hash, $cvsfile, $errorfile ); open $cvsfile, $file_ or confess "Unable to open $file_\n"; open $errorfile, ">", "${file_}\.err" or confess "Unable to open ${file_}\.err\n"; for (<$cvsfile>) { chomp($_); s/"//g; $line++; my (@linedata) = split /,/, $_; # Make a best guess (using line 1) if ( $line == 1 and ! defined ( $numfields_ ) ) { $numfields_ = scalar(@linedata) } unless ( scalar(@linedata) == $numfields_ ) { print $errorfile "$_\n"; next; } my $fieldnum = 0; for my $info (@linedata) { $fieldnum++; $hash{$line}{$fieldnum} = $info; } } close ($cvsfile); close ($errorfile); return %hash or confess "unable to return\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: build hash from csv file
by davorg (Chancellor) on Sep 19, 2005 at 13:21 UTC | |
by tcf03 (Deacon) on Sep 19, 2005 at 13:24 UTC | |
|
Re: build hash from csv file
by dragonchild (Archbishop) on Sep 19, 2005 at 13:29 UTC |