Re: hash slices



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
.



Relevant Pages

  • Re: A list with periodic boundary conditions
    ... needs to know that it is a slice. ... that changes to the slice will change the original, unlike normal lists!) ... passes requests up to its parent. ... and stashes the parental indices in it. ...
    (comp.lang.python)
  • Re: Question about idioms for clearing a list
    ... lists, dictionaries, and sets. ... above(mapping don't support slicing), until after I read the language ... constructed from the slice list, ... the key is a tuple containing the conversion of the ...
    (comp.lang.python)
  • Re: Question about idioms for clearing a list
    ... lists, dictionaries, and sets. ... above(mapping don't support slicing), until after I read the language ... constructed from the slice list, ... passed through the mapping interface, but it is not a mapping in any ...
    (comp.lang.python)
  • Re: Broken Partition
    ... message) is is preferred on this and many other lists. ... When you say "add the space to my freebsd partition" what exactly did ... I deleted and recreated ad0s3 in fdisk. ... If your new free space had been _after_ the FreeBSD slice on the disk you ...
    (freebsd-questions)
  • Re: slice notation as values?
    ... but the syntax for the slice only applies inside the square ... If you allowed a:b you get syntactic ambiguities: e.g. is this a ... > there then you don't get any syntax ambiguities, ...
    (comp.lang.python)