Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

access array of hash?

by DuncIT (Initiate)
on Mar 22, 2012 at 19:49 UTC ( #961086=perlquestion: print w/ replies, xml ) Need Help??
DuncIT has asked for the wisdom of the Perl Monks concerning the following question:

I have a variable, $rtn, that looks like this when I print it with Data Dumper:
$VAR1 = [ { 'ship_cost' => '80.84', 'ServiceType' => 'FIRST_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 8:00 or 8:30 am', 'ship_tag' => 'First Overnight' }, { 'ship_cost' => '43.71', 'ServiceType' => 'PRIORITY_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 10:30 am', 'ship_tag' => 'Priority Overnight' }, { 'ship_cost' => '38.74', 'ServiceType' => 'STANDARD_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 3:00 pm', 'ship_tag' => 'Standard Overnight' }, { 'ship_cost' => '17.5', 'ServiceType' => 'FEDEX_2_DAY', 'ship_note' => '2 Business Days Delivery by 4:30 pm', 'ship_tag' => 'Fedex 2 Day' }, { 'ship_cost' => '13.63', 'ServiceType' => 'FEDEX_EXPRESS_SAVER', 'ship_note' => '3 Business Days Delivery by 4:30 pm', 'ship_tag' => 'Fedex Express Saver' }, { 'ship_cost' => '7.75', 'ServiceType' => 'FEDEX_GROUND', 'ship_note' => '1-5 Business Days Delivery day based on di +stance to destination', 'ship_tag' => 'Fedex Ground' } ];
I've tried everything I can think of to access the nested contents. I need to know the ship_cost for the Fedex 2 Day service. How do I go about referencing that entry? Thank you so much for your help. Dunc

Comment on access array of hash?
Download Code
Re: access array of hash?
by toolic (Chancellor) on Mar 22, 2012 at 20:16 UTC
    use warnings; use strict; my $rtn = [ { 'ship_cost' => '80.84', 'ServiceType' => 'FIRST_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 8:00 or 8:30 am', 'ship_tag' => 'First Overnight' }, { 'ship_cost' => '43.71', 'ServiceType' => 'PRIORITY_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 10:30 am', 'ship_tag' => 'Priority Overnight' }, { 'ship_cost' => '38.74', 'ServiceType' => 'STANDARD_OVERNIGHT', 'ship_note' => 'Overnight Delivery by 3:00 pm', 'ship_tag' => 'Standard Overnight' }, { 'ship_cost' => '17.5', 'ServiceType' => 'FEDEX_2_DAY', 'ship_note' => '2 Business Days Delivery by 4:30 pm', 'ship_tag' => 'Fedex 2 Day' }, { 'ship_cost' => '13.63', 'ServiceType' => 'FEDEX_EXPRESS_SAVER', 'ship_note' => '3 Business Days Delivery by 4:30 pm', 'ship_tag' => 'Fedex Express Saver' }, { 'ship_cost' => '7.75', 'ServiceType' => 'FEDEX_GROUND', 'ship_note' => '1-5 Business Days Delivery day based on di +stance to destination', 'ship_tag' => 'Fedex Ground' } ]; for (@{ $rtn }) { if ($_->{ServiceType} eq 'FEDEX_2_DAY') { print "$_->{ship_cost}\n"; } } __END__ 17.5
Re: access array of hash?
by Happy-the-monk (Monsignor) on Mar 22, 2012 at 20:21 UTC

    the answers are in perldoc perldsc and perldoc perllol

    Assuming your array is called @array, this might do it:

    for my $i ( 0 .. $#array ) { next unless $array[$i]->{ServiceType} eq 'FEDEX_2_DAY'; print $array[$i]->{ship_cost} and last; }

    Cheers, Sören

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961086]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2014-10-24 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls