Re: Can't understand this.



On Thursday 29 November 2007 06:29, AY wrote:

I'm not sure what this piece of code does: Infact it is getting a
database query and iterating ...

my %temp;

while ( my %row = $dbproc->dbnextrow(1) ){

$dbproc->dbnextrow(1) is returning a list and that list is assigned to
the lexical hash %row which is only visible inside the while loop. If
nothing is assigned to %row then %row will be false and the loop will
end.


$temp{ $row{ i } } = { %row }; // No idea what this is doing.

The value of $row{ i } is used as the key for the hash %temp and it is
assigned a hash reference by copying the hash %row into an anonymous
hash. Because the %row hash is local to the while loop you can skip
the copying and just assign a hash reference:

$temp{ $row{ i } } = \%row;


}



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Cant understand this.
    ... the lexical hash %row which is only visible inside the while loop. ... The value of $rowis used as the key for the hash %temp and it is ... assigned a hash reference by copying the hash %row into an anonymous ...
    (perl.beginners)
  • Re: Noob! Help required and would be really appreciated!
    ... You've read 60% of "Learning Perl"? ... On finding the keyword "New Order", it has to start building a hash. ... want to build a loop around this hash creation. ... transferred to %allOrders. ...
    (comp.lang.perl.misc)
  • Re: multiline matching
    ... > City: Austin ... You never had more than one entry in the hash at a time. ... The for loop below is in the wrong spot. ... That loop fetches all the keys of the hash and then prints the Name, ...
    (perl.beginners)
  • Re: My hash table is in need of peer review
    ... struct hash { ... of the two possible loop exit conditions caused the loop to ... You might want to consider keeping the lists sorted ... same value pointer the caller hands you. ...
    (comp.lang.c)
  • Re: Using lock_keys with strict and bless
    ... > use strict; ... You seem to be confused about how Perl modules work. ... $this as a hash, because it isn't a hash reference - or any reference ...
    (comp.lang.perl.misc)