I'm sure Tie::CSV_file is a fine module, but you might also give Parse::CSV a spin. Here's some cutting and pasting of some code I wrote recently.
use Parse::CSV;
# Define the names of our csv fields
#
my $csv_fields = [
'CoordinatorID',
'CompanyName',
'FirstName',
'LastName',
'City',
'State',
'csgaccount',
'Username',
'DomainName'
];
# Populate these hashrefs with data for comparison with the
# list of names.
#
my $provisioned = fetch_csv_accounts({ filename => $opts->{prov},
fields => $csv_fields });
# {{{ sub fetch_csv_accounts
#
sub fetch_csv_accounts {
my $args = shift;
die "No field names provided...\n" unless defined $args->{fields};
die "No filename provided...\n" unless defined $args->{filename
+};
# Default the keyfield to Username if nothing
# is passed to this sub.
#
my $keyfield = $args->{keyfield}
? $args->{keyfield}
: 'Username';
#
# Create a Parse::CSV object for easy csv iteration
#
my $prov_csv = Parse::CSV->new( file => $args->{filename},
fields => $args->{fields}, );
die "Error: " . $prov_csv->errstr . "\n" if $prov_csv->errstr;
# Loop through each row of the csv file and populate the
# hashref. The resulting hashref will look like:
#
# Username1 -> CoordinatorID
# -> CompanyName
# -> FirstName
# -> Lastname
# -> City
# -> State
# -> csgaccount
# -> Username
# -> DomainName
# Username2 -> CoordinatorID
# -> CompanyName
# ... etc ...
my $return_hash = {};
while ( my $row = $prov_csv->fetch ) {
foreach ( keys %{$row} ) {
$return_hash->{ $row->{$keyfield} }->{$_} = $row->{$_};
}
}
return $return_hash;
} # }}}
--
naChoZ
Therapy is expensive. Popping bubble wrap is cheap. You choose.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|