Re: looping over an array of hashes
- From: shawnhcorey@xxxxxxxx (Mr. Shawn H. Corey)
- Date: Fri, 28 Apr 2006 11:12:49 -0400
On Fri, 2006-28-04 at 15:40 +0100, Graeme McLaren wrote:
Hi all, I need to loop over an array of hashes and assign a new hashref if a
condition is met:
I have a scalar which contains an array of hashes:
$locations = [
{
'location_name' => 'Fionas House',
'location_id' => '9999000000000027'
},
{
'location_name' => 'Central Locations',
'location_id' => '9999'
},
{
'location_name' => 'The Queen\'s Hoose',
'location_id' => '1076'
},
{
'location_name' => 'Santa',
'location_id' => '1075'
},
]
I want to add an new hash ref in if, for example,
if($x ==9999000000000027){
$locations->[0]->{'location_status'} = 'true'
}
I've been at this a few hours and I'm not getting anywhere, can anyone
suggest a way to do this?
An reference to an array can be dereferenced to an array:
for my $item ( @$locations ){ # $item references each hash in the list
if( $item->{location_id} == 9999000000000027 ){
$item->{location_status} = 'true';
}
}
--
__END__
Just my 0.00000002 million dollars worth,
--- Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
.
- References:
- looping over an array of hashes
- From: Graeme McLaren
- looping over an array of hashes
- Prev by Date: Re: Chomp method
- Next by Date: Re: looping over an array of hashes
- Previous by thread: looping over an array of hashes
- Next by thread: Re: looping over an array of hashes
- Index(es):
Relevant Pages
|