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

  • Home
  • SEARCH
  • 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 6172997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:29:29+00:00 2026-05-23T23:29:29+00:00

I am a bit new to Perl and I need some help regarding moving

  • 0

I am a bit new to Perl and I need some help regarding moving my Hash of Arrays across Modules.

Currently I have a db module that stores an array like so:

    sub getSourceCriteria {

    my($self) = shift();
    my($sourceId) = shift();
    chomp $sourceId;

    my(%criteria) =();
    $logger->debug("Getting records for Source ID: " . $sourceId);
    $dbh=DBI->connect('dbi:ODBC:StkSkrnDB', 'RTETET', 'XXuser01',{ RaiseError => 1, AutoCommit => 0 }) || \ 
               $logger->err_die("Database connection not made: $DBI::errstr\n");

    my($sth) =    "select a.criteria_id, a.criteria_type, a.criteria_props,a.generalcriteria_id,b.field_id ";
    $sth = $sth . "from t_criteria a, t_sourceMapping b where a.generalcriteria_id = (select generalcriteria_id from t_sourcecriteria where source_id =?) ";
    $sth = $sth . "and a.criteria_id=b.criteria_id";


    my($qry) = $dbh->prepare($sth);
    $qry->execute($sourceId) || $logger->error("Could not query for Source Criteria: $DBI::errstr\n");
    my(@row)=();
    my($tempCount) = 0;

    while ( @row = $qry->fetchrow_array ) {
        $tempCount = scalar @row;
        $logger->debug("Size of retrieved SQL Array : $tempCount");
        $criteria{$row[0]} = \@row;
        ##@{$criteria{$row[0]} } = \@row;


  }

  return %criteria;
}

And I have a seperate perl script that reads the SQL output from the code above:

    foreach my $criteria (keys %criterias) { 
        @temp = exists( $criterias{$criteria} ) ? @{ $criterias{$criteria} } : ();
        ##my $tempStr = exists( $criterias{$criteria} ) ? "Yes" : "No";
        $arraySize = scalar @temp;
        $logger->debug("GENERALCRITERIA_ID is $GENERALCRITERIA_ID and size of array is $arraySize and $temp[0]");
        $genCrit_ID = $temp[$GENERALCRITERIA_ID];
        $logger->debug("Criteria ID $criteria has Gen Criteria ID $genCrit_ID");
        if (0!=$generalCriteria_ID || $generalCriteria_ID != $genCrit_ID ) { ## test for uniqueness
            $generalCriteria_ID = -1;
        }
        else {
            $generalCriteria_ID = $genCrit_ID;
        }
    }# do something with $key and $value 
    $generalCriteria = $generalCriteria_ID;

}

The problem is I keep getting 0 as the retrieved array size( 2nd snippet) even though when I store the array ( in the 1st snippet ) I check and get the actual array size.

Please any help/clarification would be greatly appreciated.

EDIT
Added more code in the DB interface code.

  • 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-23T23:29:29+00:00Added an answer on May 23, 2026 at 11:29 pm

    In your while loop, you are assigning to @row and then storing a reference to that array. However, each time the loop iterates, you are replacing the contents of @row without declaring a new array. So at the end, each of your references point towards the same thing.

    In your code here:

    my(@row)=();
    my($tempCount) = 0;
    
    while ( @row = $qry->fetchrow_array ) {
        $tempCount = scalar @row;
        $logger->debug("Size of retrieved SQL Array : $tempCount");
        $criteria{$row[0]} = \@row;
        ##@{$criteria{$row[0]} } = \@row;
    }
    

    Each time the while loop iterates, you assign new values to the @row array. But since the my(@row)=(); line occurs outside of the loop, the @row array is always the same. So each time you assign to the array, you are changing what is stored in all of the references you have already taken.

    To fix the problem, you need to declare a new array for each iteration. The simplest way to do this is to move the declaration into the while condition:

    my($tempCount) = 0;
    
    while ( my @row = $qry->fetchrow_array ) {
        $tempCount = scalar @row;
        $logger->debug("Size of retrieved SQL Array : $tempCount");
        $criteria{$row[0]} = \@row;
        ##@{$criteria{$row[0]} } = \@row;
     }
    

    Now each time you take the reference \@row you will be getting a reference to a new array.


    If your $qry->fetchrow_array method returned an array reference, you would not have had the issue:

    my $row;
    while ($row = $qry->fetchrow_array) {
        $logger->debug("Size of retrieved SQL Array : ".@$row);
        $criteria{$$row[0]} = $row;  # already a reference
     }
    

    But I would still write that as while (my $row = ... in my own code, since keeping scopes small is a good thing.

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

Sidebar

Related Questions

I'm new to Perl and was experimenting around a bit. I have this code:
I'm looking for suggestions on new development system from some programmers that have more
We've been asked to support some rather old Perl forms on a new site,
A bit new to styling sheets. I have a div for my site's content.
I need to write some scripts for WinXP to support some of the analysts
I have a card game - written in Perl and with few objects, like
I've encountered a scenario where I'm building a Perl module as part of another
a bit new to CI, googled and overflowed alot and still got no answer
I have an array of HTML::Element s obtained from HTML::TreeBuilder and HTML::Element->find and I
I have an app that I pack into binary form using PerlApp for distribution.

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.