T
Tim Sheets
Hello group,
I am rather new to Perl (and programming in general) and am having a
problem building a hash. I have read several tutorials, examples, etc..
but nothing touches on exactly what I am trying to do.
I am trying to read data in from a CSV file (Excel export) and store the
information in an array of hashes. The first line of the file contains
field names, and the following lines contain data.
So, I am reading the first line into an array, splitting on the commas,
to use as the hash keys. Then all subsequent lines are read into
another array, splitting on the commas to be used as the values.
Then I am trying to build another array, and combine the 'keys' and
'values' from the first two arrays as a hash.
Here is the code I am trying to use:
# Begin reading csv dump
open (REFERENCEFILE,"$ARGV[1]") ;
$count=0;
while (<REFERENCEFILE>) {
if ($count == 0) {
@refdatakeys = split(/,/, $_);
$numelements = scalar (@refdatakeys);
} else {
@refdatavalues = split(/,/, $_);
for ($i=0; $i < $numelements; $i++) {
@arrefdata = ({$refdatakeys[$i] => '$refdatavalues[$i]'});
}
}
print ("SITE ID is: $arrefdata[$count]{'SITE ID'}\n");
$count++;
}
If I put print statements to test the values in @refdatakeys and
@refdatavalues, everything is stored in those two arrays. But, I can't
figure out what I am doing wrong when trying to store those values as
key/value pairs in @arrefdata. Or, I guess it's possible that values
are stored, but I am not correctly accessing that data in my print
statement.
If anyone can figure out what I am doing wrong, I sure would appreciate
some help.
Thanks all!!
Tim
I am rather new to Perl (and programming in general) and am having a
problem building a hash. I have read several tutorials, examples, etc..
but nothing touches on exactly what I am trying to do.
I am trying to read data in from a CSV file (Excel export) and store the
information in an array of hashes. The first line of the file contains
field names, and the following lines contain data.
So, I am reading the first line into an array, splitting on the commas,
to use as the hash keys. Then all subsequent lines are read into
another array, splitting on the commas to be used as the values.
Then I am trying to build another array, and combine the 'keys' and
'values' from the first two arrays as a hash.
Here is the code I am trying to use:
# Begin reading csv dump
open (REFERENCEFILE,"$ARGV[1]") ;
$count=0;
while (<REFERENCEFILE>) {
if ($count == 0) {
@refdatakeys = split(/,/, $_);
$numelements = scalar (@refdatakeys);
} else {
@refdatavalues = split(/,/, $_);
for ($i=0; $i < $numelements; $i++) {
@arrefdata = ({$refdatakeys[$i] => '$refdatavalues[$i]'});
}
}
print ("SITE ID is: $arrefdata[$count]{'SITE ID'}\n");
$count++;
}
If I put print statements to test the values in @refdatakeys and
@refdatavalues, everything is stored in those two arrays. But, I can't
figure out what I am doing wrong when trying to store those values as
key/value pairs in @arrefdata. Or, I guess it's possible that values
are stored, but I am not correctly accessing that data in my print
statement.
If anyone can figure out what I am doing wrong, I sure would appreciate
some help.
Thanks all!!
Tim