Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:19:11+00:00 2026-05-15T04:19:11+00:00

I’m doing a little datamining project where a perl script grabs info from a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:19:12+00:00Added an answer on May 15, 2026 at 4:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.