Re: hash slices
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Thu, 28 Sep 2006 12:49:47 -0700
Gerald Host wrote:
I'm using DBI's selectall_hashref with 5 key columns. I want to display
each row where key col 1 is 'yes' or NULL (undef or '' ?)
foreach my $medCategory (keys %{$href->{'yes'}->{qw('' 'yes')}->{qw('' 'yes')}}) {
foreach my $med (keys %{$href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}}) {
foreach my $ID (keys %{$href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}}) {
print qq{ $href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}->{$ID}->{ID} };
print qq{ $href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}->{$ID}->{Medication} };
}
}
}
This isn't working of course because I don't quite understand the syntax I
need. Can someone give me a hint? Thanks!
A slice of a variable has to start with @, for example:
@array_slice[ 1 .. 9 ];
@hash_slice{ 'one', 'two', 'three' };
If you have a multidimensional data structure you can only slice at the end:
@{ $array_ref->[ 3 ][ 7 ] }[ 1 .. 9 ];
@{ $hash_ref->{ x }{ y } }{ 'one', 'two', 'three' };
In your example:
%{ $href->{ 'yes' }->{ qw( '' 'yes' ) }->{ qw( '' 'yes' ) } }
There is no slice, and the lists evaluate to the last item in the list so what
you actually have is:
%{ $href->{ 'yes' }->{ "'yes'" }->{ "'yes'" } }
And yes, the single quotes are included in the key as that is the way qw() works.
It looks like you want something like this:
for my $href1 ( @{ $href->{ yes } }{ '', 'yes' } ) {
for my $href2 ( @{ $href1 }{ '', 'yes' } ) {
for my $medCategory ( keys %$href2 ) {
for my $med ( keys %{ $href2->{ $medCategory } } ) {
for my $ID ( keys %{ $href2->{ $medCategory }{ $med } } ) {
print " $href2->{ $medCategory }{ $med }{ $ID }{ ID } ";
print " $href2->{ $medCategory }{ $med }{ $ID }{
Medication } ";
}
}
}
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.
- References:
- hash slices
- From: Gerald Host
- hash slices
- Prev by Date: Re: interpoliation within regexp
- Next by Date: Re: interpoliation within regexp
- Previous by thread: Re: hash slices
- Next by thread: Fancy A Challenge!
- Index(es):
Relevant Pages
|