views:

133

answers:

1

I'm doing a little datamining project where a perl script grabs info from a SQL database and parses it. The data consists of several timestamps. I want to find how many of a particular type of timestamp exist on any particular day. Unfortunately, this is my first perl script, and the nature of perl when it comes to hashes and arrays is confusing me quite a bit.

Code segment:

my %values=();#A hash of the total values of each type of data of each day.
#The key is the day, and each key stores an array of each of the values I need.
my @proposal;
#[drafted timestamp(0), submitted timestamp(1), attny approved timestamp(2),Organiziation approved timestamp(3), Other approval timestamp(4), Approved Timestamp(5)]
while(@proposal=$sqlresults->fetchrow_array()){
 #TODO: check to make sure proposal is valid
 #Increment the number of timestamps of each type on each particular date
 my $i;
for($i=0;$i<=5;$i++)
$values{$proposal[$i]}[$i]++;
#Update rolling average of daily 
#TODO: To check total load, increment total load on all dates between attourney approve date and accepted date
for($i=$proposal[1];$i<=$proposal[2];$i++)
 $values{$i}[6]++; 
}

I keep getting syntax errors inside the for loops incrementing values. Also, considering that I'm using strict and warnings, will Perl auto-create arrays of the right values when I'm accessing them inside the hash, or will I get out-of bounds errors everywhere?

Thanks for any help, Zach

+5  A: 
amphetamachine
$values{$proposal[$i]}[$i] is perfectly fine. You only need the -> arrow to dereference a scalar at the beginning. Eg: my $arr_ref=[ { a=>'f', b=>'g'}, { c=>'h', d=>'i' }]; print $arr_ref->[0]{b};
DougWebb
@DougWebb - You're perfectly right; Perl does compensate for the lack of a `->` in the expression, even in `use strict`. I'll leave it as-is to give the OP a sense of what is going on as far as the data structures go.
amphetamachine
I've got a pretty good idea of how the structures work now. My code actually works, and looks quite a bit cleaner as well! I'm still figuring out when Perl does and doesn't do things automatically, but this is a good push in the right direction. Thanks all!
Zach H
please remember to accept an answer
stocherilac