CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Getting Values from Hash

 



woodpecker
New User

Jun 5, 2009, 12:42 PM

Post #1 of 6 (1127 views)
Getting Values from Hash Can't Post

Hi folks,

I have a hash called @cart as follows:-

[
{
'mv_ib' => 'products',
'mv_sku' => '',
'quantity' => '1',
'mv_ip' => '0',
'gift_cert' => '',
'option_type' => '',
'price_group' => 'general',
'code' => 'os28080'
},
{
'mv_ib' => 'products',
'mv_sku' => '',
'mv_ip' => '1',
'quantity' => '1',
'gift_cert' => '',
'option_type' => '',
'price_group' => 'general',
'code' => 'os28065'
}
]


I am trying to get the 'code' values as follows:-

foreach $item (@cart)
{
$itm .= $item->{'code'};
}

This gives an error

Bad index while coercing array into hash

Please help,

Thanks


1arryb
User

Jun 5, 2009, 1:09 PM

Post #2 of 6 (1126 views)
Re: [woodpecker] Getting Values from Hash [In reply to] Can't Post

Hi woodpecker,

Er, '@cart' is an array, not a hash. Try changing the '@' with a '%' and run your program again.

Also, hash keys are implicitly quoted, so $item->{'code'}; is redundant.

UPDATE:

Oy! I didn't look at this one closely enough:

"[]" means "This is a reference to an array"
"{}" means "This is a reference to a hash"

So, the value you posted is actually "a reference to an array of hash references". If this is true then you need something like:

Code
foreach $item (@$cart) 
{
$itm .= $item->{'code'};
}


Otherwise, @cart doesn't have the value you say it has.

Cheers,

Larry


(This post was edited by 1arryb on Jun 5, 2009, 1:23 PM)


woodpecker
New User

Jun 5, 2009, 1:43 PM

Post #3 of 6 (1118 views)
Re: [1arryb] Getting Values from Hash [In reply to] Can't Post

Hi Larry,

Well I changed it to @$cart as you suggested, the error goes but it doesn't work, the array I posted is as Data::Dumper prints it. Any other suggestions?

Andy.


KevinR
Veteran


Jun 5, 2009, 1:56 PM

Post #4 of 6 (1115 views)
Re: [woodpecker] Getting Values from Hash [In reply to] Can't Post

Code seems to work:


Code
use warnings; 
use strict;
my $cart = [
{
'mv_ib' => 'products',
'mv_sku' => '',
'quantity' => '1',
'mv_ip' => '0',
'gift_cert' => '',
'option_type' => '',
'price_group' => 'general',
'code' => 'os28080'
},
{
'mv_ib' => 'products',
'mv_sku' => '',
'mv_ip' => '1',
'quantity' => '1',
'gift_cert' => '',
'option_type' => '',
'price_group' => 'general',
'code' => 'os28065'
}
];

my $itm;

foreach my $item (@{$cart}) {
$itm .= $item->{code};
}


When I run the above with "strict" on it runs OK, but if I run it with "strict" off it returns a syntax error. Not sure why that is.
-------------------------------------------------


woodpecker
New User

Jun 5, 2009, 2:09 PM

Post #5 of 6 (1111 views)
Re: [KevinR] Getting Values from Hash [In reply to] Can't Post

Hi KevinR,

Thanks for the reply, my array or hash reference (stil confused as to exactly which it is) was stored in @cart, I noticed in your example it was in $cart, now I have changed this it works just fine.

Many thanks!


KevinR
Veteran


Jun 5, 2009, 2:17 PM

Post #6 of 6 (1109 views)
Re: [woodpecker] Getting Values from Hash [In reply to] Can't Post


In Reply To
Hi KevinR,

Thanks for the reply, my array or hash reference (stil confused as to exactly which it is) was stored in @cart, I noticed in your example it was in $cart, now I have changed this it works just fine.

Many thanks!


If its a reference (which it is) its actually stored in a scalar, which is why I used $cart instead of @cart.

A reference is a scalar that points to something else, like an array or hash, among other things. If you print the value of just $cart it will retun something like:

ARRAY(0x15959b0)

the type of thing the scalar points to is the first thing (ARRAY) and the rest of the stuff in parenthisis must be the address in memory where the data is stored. So essentially a reference is a pointer to a 'type' of something and the address where that something can be found.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives