Question

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

Was it helpful?

Solution

Errors:

for($i=0;$i<=5;$i++)
    $values{$proposal[$i]}[$i]++;
for($i=$proposal[1];$i<=$proposal[2];$i++)
    $values{$i}[6]++; 

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:

for($i=0;$i<=5;$i++) {
    $values{$proposal[$i]}[$i]++;
}
for($i=$proposal[1];$i<=$proposal[2];$i++) {
    $values{$i}[6]++;
}

$values{$proposal[$i]}[$i]++;

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:

my $arr_ref = [];               # empty array reference
my $arr_ref = [ 1, 2, 'foo', ]; # initialize with values
my $arr_ref = \@arr;            # reference an existing array;
                                # does not make copy, but provides a
                                # read-write handle to the array

$arr_ref->[0];                  # index the first (index 0) element of the array
@{$arr_ref}[ 0 .. 4 ];          # index elements number one through five (0-4) of the array
                                # through what's called an "array slice"

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:

                    # v-- note the arrow
$values{$proposal[$i]}->[$i]++;

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 that for and foreach are 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 for loops. There are a few habits that can result in faster programs overall if they have a lot of long for loops. For instance:

    • ++$foo is more efficient than $foo++. This stems from the internals:
      • $foo++ increments the variable, subtracts 1 from it, then returns the result, whereas
      • ++$foo increments the variable and returns it. Fewer operations = faster.
    • A less-than-or-equals comparison is less efficient than a plain less-than comparison. Again, this is due to the number of operations you computer has to perform. for ($x=0; $x<=5; ++$x) is better-written as for ($x=0; $x<6; ++$x).
  • Perl has some wonderful loop controls. Some, like map, are very powerful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top