Richard Heintze wrote:
Is there a way I can explictly declare that each array
cell contains a hash?


Only with a loop of some sort.


Here is the only way I know to do it:

my @PCEs=[];

This does not do what you think it does. This is assigning an anonymous array reference into the first element of the array.


while ($Data->FetchRow()) {

What is 'FetchRow' doing? This is not normal DBI, and is being used in a true/false context? You also don't need the empty parens, but that is more stylistic.


  my %dh = $Data->DataHash();
  $PCEs[$cn]{$dh{"id"}} = $dh{"ridPCE"};

Where did $cn come from? Are you using strict and warnings? If not you need to be. $cn is not changing in your loop so while FetchRow is true you are going to continually overwrite the value you are assigning which will cause your array to have one value, the last. If you are changing $cn in the DataHash method you shouldn't be and have broken the encapsulation, but that is a design issue.


}

This "my" declaration only says that it is an array,
not an array of hashes. Can I improve upon this?


'my' is for defining scope and nothing more. It is how you use the variable that determines what it contains. Your assignment statement will autovivify the element in the array into a hash reference as you want so there is no need to improve it. If you want to force an array element into a hash reference then something like,


$PCEs[$cn] = {};

Will set the element of PCEs at index $cn to an empty hash reference, but isn't necessary if you use the variable initially as above.

perldoc -f my
perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc

You should read through the docs above, it will help you understand complex data structures in Perl. You may also want to look at,

perldoc Data::Dumper

To help you visualize what your data structures contain. And ALWAYS do,

use strict;
use warnings;

At the top of your code....

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to