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
Errors:
Perl does not support bare loop/conditional blocks. Or rather, it does, but not like this. This may work in PHP, but not in Perl. You will want to enclose these in blocks:
Since hashes in Perl can only fit scalar data types in them, in order to store an entire array inside of a hash, we’re going to have to do it by reference. Here’s a quick tutorial on array references:
What your code above does is pull the value at hash key
$proposal[$i]out of the hash%values, then use it (a scalar) as an array (it is not an array).As I said before, you can use it as an array reference but not an array:
Suggestions:
Writing
my $foo; for ($foo = 0; $foo <= 5; $foo++)is more easily written as “for my $foo (0 .. 5)” or “foreach my $foo (0 .. 5)“. This is, in essence how most people do it. Of note is thatforandforeachare interchangeable–it’s a matter of preference and legibility.Please, for legibility’s sake, indent your code with more than one space. A good rule of thumb is four spaces, or a tab. St. Larry Wall was thinking of languages people speak and write when he designed Perl.
I’d recommend researching the proper (proper, here, meaning most efficient) way to write
forloops. There are a few habits that can result in faster programs overall if they have a lot of longforloops. For instance:++$foois more efficient than$foo++. This stems from the internals:$foo++increments the variable, subtracts 1 from it, then returns the result, whereas++$fooincrements the variable and returns it. Fewer operations = faster.for ($x=0; $x<=5; ++$x)is better-written asfor ($x=0; $x<6; ++$x).Perl has some wonderful loop controls. Some, like
map, are very powerful.