Re: looping over an array of hashes

2006-04-28 Thread Mr. Shawn H. Corey
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' => '0027'
>   },
>   {
> 'location_name' => 'Central Locations',
> 'location_id' => ''
>   },
>   {
> '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 ==0027){
> $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} == 0027 ){
$item->{location_status} = 'true';
  }
}

-- 
__END__

Just my 0.0002 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/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




looping over an array of hashes

2006-04-28 Thread Graeme McLaren
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' => '0027'
 },
 {
   'location_name' => 'Central Locations',
   'location_id' => ''
 },
 {
   '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 ==0027){
$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?



Cheers,

G :)



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Looping over an array of hashes problem

2005-09-08 Thread John W. Krahn
Graeme McLaren wrote:
> Morning all, I have a problem that I can't see a way around.  Basically
> I have an array of hashes and I want to get the key and value of each
> hash but with the following code I'm getting:
> 
> Type of arg 1 to keys must be hash (not array element) at
> /usr/lib/perl5/vendor_perl/Purchaser/Common.pm line 477, near "]) "
> 
> #code:
> 
> foreach my $key (keys $AoH[$map_loop{$i}]) {
> my $value = $AoH[$map_loop{$i}]->{$key};
> 
>  }
> 
> 
> Is there a way to loop over an array of hashes?

foreach my $key ( keys %{ $AoH[ $map_loop{ $i } ] } ) {
my $value = $AoH[ $map_loop{ $i } ]{ $key };

 }


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Looping over an array of hashes problem

2005-09-08 Thread Jeff Pan

maybe u would try:

foreach my $key (keys %{$AoH[$map_loop{$i}]}) {
...
}

because $AoH[$map_loop{$i}] is just a ref,so u get wrong.

On Thu, 08 Sep 2005 09:42:33 +0100, "Graeme McLaren"
<[EMAIL PROTECTED]> said:
> Morning all, I have a problem that I can't see a way around.  Basically I 
> have an array of hashes and I want to get the key and value of each hash
> but 
> with the following code I'm getting:
> 
> Type of arg 1 to keys must be hash (not array element) at 
> /usr/lib/perl5/vendor_perl/Purchaser/Common.pm line 477, near "]) "
> 
> #code:
> 
> foreach my $key (keys $AoH[$map_loop{$i}]) {
>  my $value = $AoH[$map_loop{$i}]->{$key};
> 
>   }
> 
> 
> Is there a way to loop over an array of hashes?
> 
> 
> Cheers for any light you can shed on this?
> 
> 
> Cheers,
> 
> Graeme :)
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 
> 
-- 
  Jeff Pan
  [EMAIL PROTECTED]

-- 
http://www.fastmail.fm - mmm... Fastmail...


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Looping over an array of hashes problem

2005-09-08 Thread Graeme McLaren
Morning all, I have a problem that I can't see a way around.  Basically I 
have an array of hashes and I want to get the key and value of each hash but 
with the following code I'm getting:


Type of arg 1 to keys must be hash (not array element) at 
/usr/lib/perl5/vendor_perl/Purchaser/Common.pm line 477, near "]) "


#code:

foreach my $key (keys $AoH[$map_loop{$i}]) {
my $value = $AoH[$map_loop{$i}]->{$key};

 }


Is there a way to loop over an array of hashes?


Cheers for any light you can shed on this?


Cheers,

Graeme :)



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: looping over an array of hashes

2001-06-01 Thread Walt Mankowski

On Fri, Jun 01, 2001 at 03:29:43PM -0700, Chuck Ivy wrote:
> First post, quick question:
> 
> I've got an array of hashes that I'm defining the most basic way I can...
> 
> my $gSeasonID;
> my @season_list = '';
> while (@fields = $sth->fetchrow_array) {
>   $gSeasonID = $fields[0];
>   $season_list[$gSeasonID]{number} = $fields[1];
>   $season_list[$gSeasonID]{title} = $fields[2];
>   $season_list[$gSeasonID]{active} = $fields[3];
> }
> 
> where @fields is coming from a DBI query.
> 
> Essentially I have
> 
> $season_list[1]{number} = 1;
> $season_list[1]{title} = 'Season One';
> $season_list[1]{active} = 0;
> $season_list[2]{number} = 1;
> $season_list[2]{title} = 'Season Two';
> $season_list[2]{active} = 1;
> 
> That's fine, and it seems to be writing correctly. But I'd like to loop 
> on my array index later to get back, say {title} from each season.
> 
> Is there a clean way to do this with foreach or while? What I'd be 
> looking for would be $season_list[$loop_season]{name}, if I were using a 
> for loop with $loop_season as my index. Is there a way to do this 
> with $_ or something and foreach so that I don't have to know the size 
> of my array?

Each element in @season_list is just a reference to a hash, so you can
loop through it with foreach using code like this:

foreach my $sl (@season_list) {
print "$sl->{title}\n";
}





Re: looping over an array of hashes

2001-06-01 Thread Bill Stilwell

On Fri, Jun 01, 2001 at 03:29:43PM -0700, Chuck Ivy wrote:
> First post, quick question:
> 
> I've got an array of hashes that I'm defining the most basic way I can...
> 
> my $gSeasonID;
> my @season_list = '';
> while (@fields = $sth->fetchrow_array) {
>   $gSeasonID = $fields[0];
>   $season_list[$gSeasonID]{number} = $fields[1];
>   $season_list[$gSeasonID]{title} = $fields[2];
>   $season_list[$gSeasonID]{active} = $fields[3];
> }

First things first, you don't really nead $gSeasonID, this will work
just as well:

$season_list[$fields[0]] = $fields[1]; 

Second of all, I think this problem becomes a lot easier if you use
hashes of hashes, like so:

$season_list{$fields[0]}{number} = $fields[1];

Because now you can loop over the seasons like so:

foreach (keys %season_list) {
print $season_list{$_}{title}; # or do whatever to this data
}

Now you don't need to fuss with array size or index values at all.

Hope this helps!

Bill
-- 
Bill Stilwell   
[EMAIL PROTECTED]
It's all margins.
Oh, just read my weblog: http://www.marginalia.org



Re: looping over an array of hashes

2001-06-01 Thread Chas Owens

First ugly thought goes like this:

my @season_list = '';
while (my @fields = $sth->fetchrow_array) {
for (my $i = 1; $i < @fields; $i++) {
$season_list[$fields[0]]{('number', 'title', 'active')[$i]} = $fields[$i];
}

Second thought is: 

Why not use $sth->fetchrow_hashref?  The only reason I can think of
would be a case where your select looked like this

select a.id, b.name, a.name from tab1 a, tab2 b where a.id = b.id;

in this case Informix (and many other databases) returns (id, name,
name) from the $sth->{NAME} call.  This can be corrected by writing you
sql like this (please adjust for your flavor of sql):

select a.id, b.name as b_name, a.name as a_name 
from tab1 a, tab2 b
where a.id = b.id;


On 01 Jun 2001 15:29:43 -0700, Chuck Ivy wrote:
> First post, quick question:
> 
> I've got an array of hashes that I'm defining the most basic way I can...
> 
> my $gSeasonID;
> my @season_list = '';
> while (@fields = $sth->fetchrow_array) {
>   $gSeasonID = $fields[0];
>   $season_list[$gSeasonID]{number} = $fields[1];
>   $season_list[$gSeasonID]{title} = $fields[2];
>   $season_list[$gSeasonID]{active} = $fields[3];
> }
> 
> where @fields is coming from a DBI query.
> 
> Essentially I have
> 
> $season_list[1]{number} = 1;
> $season_list[1]{title} = 'Season One';
> $season_list[1]{active} = 0;
> $season_list[2]{number} = 1;
> $season_list[2]{title} = 'Season Two';
> $season_list[2]{active} = 1;
> 
> That's fine, and it seems to be writing correctly. But I'd like to loop 
> on my array index later to get back, say {title} from each season.
> 
> Is there a clean way to do this with foreach or while? What I'd be 
> looking for would be $season_list[$loop_season]{name}, if I were using a 
> for loop with $loop_season as my index. Is there a way to do this 
> with $_ or something and foreach so that I don't have to know the size 
> of my array?

-- 
Today is Boomtime, the 6th day of Confusion in the YOLD 3167
Fnord.





looping over an array of hashes

2001-06-01 Thread Chuck Ivy

First post, quick question:

I've got an array of hashes that I'm defining the most basic way I can...

my $gSeasonID;
my @season_list = '';
while (@fields = $sth->fetchrow_array) {
$gSeasonID = $fields[0];
$season_list[$gSeasonID]{number} = $fields[1];
$season_list[$gSeasonID]{title} = $fields[2];
$season_list[$gSeasonID]{active} = $fields[3];
}

where @fields is coming from a DBI query.

Essentially I have

$season_list[1]{number} = 1;
$season_list[1]{title} = 'Season One';
$season_list[1]{active} = 0;
$season_list[2]{number} = 1;
$season_list[2]{title} = 'Season Two';
$season_list[2]{active} = 1;

That's fine, and it seems to be writing correctly. But I'd like to loop 
on my array index later to get back, say {title} from each season.

Is there a clean way to do this with foreach or while? What I'd be 
looking for would be $season_list[$loop_season]{name}, if I were using a 
for loop with $loop_season as my index. Is there a way to do this 
with $_ or something and foreach so that I don't have to know the size 
of my array?