Re: looping over an array of hashes
- From: "DJ Stunks" <DJStunks@xxxxxxxxx>
- Date: 28 Apr 2006 08:15:16 -0700
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:
C:\tmp>cat -n tmp.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Data::Dumper;
7
8 my $locations = [
9 { 'location_name' => 'Fionas House',
10 'location_id' => '9999000000000027',
11 },
12 { 'location_name' => 'Central Locations',
13 'location_id' => '9999'
14 },
15 { 'location_name' => 'The Queen\'s Hoose',
16 'location_id' => '1076'
17 },
18 { 'location_name' => 'Santa',
19 'location_id' => '1075'
20 },
21 ];
22
23 my %x = map { $_ => 1 } qw{ 9999000000000027 1075 };
24
25 for my $location ( @$locations ) {
26 $location->{location_status} = 1
27 if $x{ $location->{location_id} };
28 }
29
30 print Dumper $locations;
C:\tmp>tmp.pl
$VAR1 = [
{
'location_status' => 1,
'location_id' => '9999000000000027',
'location_name' => 'Fionas House'
},
{
'location_id' => '9999',
'location_name' => 'Central Locations'
},
{
'location_id' => '1076',
'location_name' => 'The Queen\'s Hoose'
},
{
'location_status' => 1,
'location_id' => '1075',
'location_name' => 'Santa'
}
];
HTH,
-jp
.
- Follow-Ups:
- Re: looping over an array of hashes
- From: DJ Stunks
- Re: looping over an array of hashes
- References:
- looping over an array of hashes
- From: Graeme McLaren
- looping over an array of hashes
- Prev by Date: Re: looping over an array of hashes
- Next by Date: Modifiaction time of file
- Previous by thread: Re: looping over an array of hashes
- Next by thread: Re: looping over an array of hashes
- Index(es):
Relevant Pages
|