Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

traversing an Array of Hashes 1

Status
Not open for further replies.

shankem

Programmer
Joined
Nov 28, 2006
Messages
5
Location
US
I am using DBIx::Wrapper to read in rows from a database. The DBIx::Wrapper functin 'nativeSelectMulti' returns the retrieved rows as an array of hashes. My problem is I cannot access the data in the array without errors. I use Data::Dumper to dump the data and everything seems fine. But if I try to acces it in anyway it gives errors. My error output is below as well.

My code is as such:

# Retrieve list of districts within the state the lead is from.

while (my $lead = $leads->next) {

my @districts = $db->nativeSelectMulti( "SELECT * FROM rmg.District WHERE state=:state", { state => $$lead{state} } )

or die "Couldn't select districts: $!\n";



print Dumper( The only thing that works here is @districts);

}


If I type 'print Dumper( @districts);' I get the following output.


$VAR1 = [
{
'region' => '1',
'name' => 'Jacksonville',
'phone' => undef,
'state' => 'FL',
'city' => 'Jacksonville',
'employee' => '0',
'fax' => undef,
'id' => '58',
'address' => undef
},
{
'region' => '1',
'name' => 'Miami',
'phone' => undef,
'state' => 'FL',
'city' => 'Miami',
'employee' => '0',
'fax' => undef,
'id' => '59',
'address' => undef
}
];


Nothing wrong there.

Now if I type print Dumper( $districts[0] ); I get the same exact output

$VAR1 = [
{
'region' => '1',
'name' => 'Jacksonville',
'phone' => undef,
'state' => 'FL',
'city' => 'Jacksonville',
'employee' => '0',
'fax' => undef,
'id' => '58',
'address' => undef
},
{
'region' => '1',
'name' => 'Miami',
'phone' => undef,
'state' => 'FL',
'city' => 'Miami',
'employee' => '0',
'fax' => undef,
'id' => '59',
'address' => undef
}
];



If I type print Dumper( $districts[0]->{region} ); It print the first hash, when it should print just the value for region. Right?

Pseudo-hashes are deprecated at ./dispatch line 58. Line 58 being 'Print Dumper($districts blah blah
$VAR1 = {
'region' => '1',
'name' => 'Miami',
'phone' => undef,
'state' => 'FL',
'city' => 'Miami',
'employee' => '0',
'fax' => undef,
'id' => '59',
'address' => undef
};


Furthermore if I type print Dumper( $districts[0]->{region}->{region} ) or Dumper( $districts[0]->{region}->{name} or {region}->{phone} etc etc I get the following;

Pseudo-hashes are deprecated at ./dispatch line 58.
$VAR1 = '1';


Pseudo-hashes are deprecated at ./dispatch line 58.
$VAR1 = 'Miami';


Pseudo-hashes are deprecated at ./dispatch line 58.
$VAR1 = undef;


$VAR1 is holding the correct value as you can see. That's what I'm looking for but I still get the Pseudo-hash error. And somethign is telling me $obj[0]->{value}->{value} is not the correct way to access the wanted variable. Any insight on this would be greatly appreciated.

 
$districts is a reference to an array

print Dumper ($districts->[0]);

-------------
Kirsle.net | Kirsle's Programs and Projects
 
you know I tried that but if I use 'print Dumper($districts->[0]); ' Then I get the following error output.

Global symbol "$districts" requires explicit package name at ./dispatch line 58.

even if I put

'print Dumper( $districts->[0]->{'region') ); '

I get the exact same error.

 
instead of:

$districts[0]->{region}

try:

$districts[0]{region}



- Kevin, perl coder unexceptional!
 
typo 'print Dumper($districts->[0]->{'region} ); '
 
if i type 'print Dumper ($districts[0]{region}{name});'

I get:

Pseudo-hashes are deprecated at ./dispatch line 58.
$VAR1 = 'Miami';

or if I type 'print Dumper ($districts[0]{region});'

I get:

Pseudo-hashes are deprecated at ./dispatch line 58.
$VAR1 = {
'region' => '1',
'name' => 'Miami',
'phone' => undef,
'state' => 'FL',
'city' => 'Miami',
'employee' => '0',
'fax' => undef,
'id' => '59',
'address' => undef
};

Now that's what fustrates me the data is correct and obviously is being retrieved but I still get an error and the script dies.
 
You are being confused. If you want to know the exact contents of an @array, pass it to Dumper by reference: Dumper(\@array). Otherwise, it is simply going to operate on each of the elements of the array individually.

Code:
my @array = (1..4);
print Dumper(@a)
# $VAR1 = 1;
# ,$VAR2 = 2;
# ,$VAR3 = 3;
# ,$VAR4 = 4;

However, knowing this, you can actually still decode the first output that your received. @districts contains only 1 element, and that is an array reference full of hash references. I imagine that this is not exactly what you expected, but this is why the following results are identical:

Code:
Dumper(@districts);
Dumper($districts[0]);

To reach the first hash reference, you will have to use the following command:

Code:
# First Hash reference:
print Dumper($districts[0][0]);

# Region of first Hash reference
print Dumper($districts[0][0]{region};

Or, if you want to fix your data structure so that it is of the form that you want it to be, then do the following:

Code:
@districts = @{$districts[0]};

Enjoy.
 
that was it. thanks for the insight
 
that deserved a star [flowerface]

- Kevin, perl coder unexceptional!
 
Thanks for the star Kevin.

Truth is seeing something that both you and Kirsle overlooked was pleasurable enough for me.

Star's nice too though [upsidedown]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top