values of a "subhash"

D

Dietmar Kehr

hi,

if i have a hash %myhash in, lets say tree arguments, with values

$myhash{$a}{$b}{$c}

then i can get the value-list of the "last"
subhash (fixed 1.,2. args) by

values %{$myhash{bla}{blum}}

Cool!

I'd like to get the list of the "middle" subhash
(fixed 1., 3. args). How is this possible?

Best regards.
 
S

Sherm Pendley

Dietmar Kehr said:
then i can get the value-list of the "last"
subhash (fixed 1.,2. args) by

values %{$myhash{bla}{blum}}

Cool!

I'd like to get the list of the "middle" subhash
(fixed 1., 3. args). How is this possible?

The same way:

values %{$myhash{bla}};

Keep in mind that the values are hash refs, not hashes. See perldsc
and perllol for more.

sherm--
 
A

Anno Siegel

Dietmar Kehr said:
hi,

if i have a hash %myhash in, lets say tree arguments, with values

$myhash{$a}{$b}{$c}

then i can get the value-list of the "last"
subhash (fixed 1.,2. args) by

values %{$myhash{bla}{blum}}

Cool!

I'd like to get the list of the "middle" subhash
(fixed 1., 3. args). How is this possible?

Good question. Please define your terms: What is a "subhash"? What
does it mean to "fix an arg"?

One answer could be (untested)

my %aux;
for my $k1 ( keys %myhash ) {
for my $k2 ( keys %{ $myhash{ $k1}} ) {
for my $k3 ( keys %{ $myhash{ $k1}->{ $k2} ) {
$aux{ $k1}->{ $k3}->{ $k2} = $myhash{ $k1}->>{ $k2}->{ $k3};
}
}
}

$aux{ bla}->{ blub};

The interpretation is up to you.

Anno
 
A

Anno Siegel

Sherm Pendley said:
The same way:

values %{$myhash{bla}};

Ah, but that doesn't "fix the third arg", whatever that means. I'm
not sure what the OP really wants.

Anno
 
A

Arne Ruhnau

Anno said:
Ah, but that doesn't "fix the third arg", whatever that means. I'm
not sure what the OP really wants.

Maybe

$hash{fixed1}{wildcard}{fixed3}

resulting in every $key for which

$hash{fixed1}{$key}{fixed3}

exists? Well, nevertheless the OP should clarify what he wants (and why).

Arne Ruhnau
 
P

Paul Lalli

Dietmar said:
if i have a hash %myhash in, lets say tree arguments, with values

$myhash{$a}{$b}{$c}

then i can get the value-list of the "last"
subhash (fixed 1.,2. args) by

values %{$myhash{bla}{blum}}

Cool!

I'd like to get the list of the "middle" subhash
(fixed 1., 3. args). How is this possible?

If I understand what you're looking for, you want all the values of the
form
$myhash{$a}{XXX}{$c} for all values of XXX. Is that correct?

(Note: It is generally most helpful if you can provide, at minimum, a
sample set of input and desired output. That way, we don't have to
guess as to what you're actually looking for.)

If I have interpreted your desire correctly, there is no particular
built-in way of obtaining this. In fact, I'd take a long hard look at
your data structure, and try to determine if perhaps it should be
redesigned. Regardless, you can obtain this list by looping through
the keys of the %{$myhash{$a}} hash:

my @values;
for my $key (keys %{$myhash{$a}}) {
push @values, $myhash{$a}{$key}{$c} if exists $myhash{$a}{$key}{$c};
}

The additional 'if' modifier is to ensure that each particular hash
does in fact have a key $c. If you are not concerned about this, you
could rewrite this as one 'map' statement[1]:
my @values = map {$myhash{$a}{$_}{$c}} keys %{$myhash{$a}};

Hope this helps,
Paul Lalli

[1] In truth, you could probably keep the 'if exists' logic and still
use a single statement, by adding in a grep() clause. But, IMHO, at
that point it's simply more clear to spell it out with the explicit for
loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top