Databases Reference
In-Depth Information
$Animals{$AnimalName}=$AnimalCount;
is effectively:
$Animals{cats}=2;
In this way, we add entries to the %Animals hash for each animal. The complete program
code is listed in Example 16-7.
Example 16-7. Perl script to read in data from a CSV file
#!/usr/bin/perl
use strict;
# If the user hasn't provided one command-line argument, provide a
# helpful error message.
if(@ARGV!=1)
{
die("Syntax: $0 [Input file]\n");
}
# Open the file specified on the command line; if we can't open it,
# print an error message and stop.
if(!open(INPUTFILE, $ARGV[0]))
{
die("Failed opening $ARGV[0]\n");
}
my %Animals;
# Read in from input file line by line; each line is
# automatically placed in $_
while(<INPUTFILE>)
{
# Remove the newline at the end of the line
chomp($_);
# Split the line by commas and load into the AnimalsData array
my @AnimalsData=split(",", $_);
# Assign the text before the first comma to the name
my $AnimalName=$AnimalsData[0];
# Assign the text between the first comma and the second comma
# (if any) to the count
my $AnimalCount=$AnimalsData[1];
# Add an entry to the Animals hash for this animal name and
# count pair:
$Animals{$AnimalName}=$AnimalCount;
}
# Close the input file
close(INPUTFILE);
# Process the data to calculate the total; code beyond this point is
# identical to our previous example and doesn't deal with the
# command-line arguments.
 
Search WWH ::




Custom Search