hash of hashes with lists

C

cousin_bubba

Hello,

I am a newbie with perl so please exuse me if this is a dumb question.

In my script I have a hash of hashes and the values of the internal
hashes are lists or arrays. I need to access all of the elements of
these arrays for comparison.

The hash of hashes looks like this:

%line_sec = (
one => (
WXYZ => ["ABCD", "EFGH"],
TUVW => ["TEFH", "LPCD"],
),
two => (
DVDC => ["GHIJ", "ELFO"],
LMNO => ["PQRS", "TUVW"],
)
);

Basically I want to cycle through all of the four letter accronyms to
see if they match a current value called $current_acc. The numbers
"one" "two" etc are command line arguments. The $head_acc variable is
the key of the inner hashes. I have tried the following but it
doesn't work:

foreach $match (@{$line_sec{$ARGV[0]}}{$head_acc})
{
if ($match eq $current_acc)
{
...
}
}

The array I would like to obtain for the case of "one" and "WXYZ" for
instance would be (GHIH ELFO).

Is there a way I can do this?

Thanks in advance for your help.
 
M

Matt Garrish

cousin_bubba said:
Hello,

I am a newbie with perl so please exuse me if this is a dumb question.

In my script I have a hash of hashes and the values of the internal
hashes are lists or arrays. I need to access all of the elements of
these arrays for comparison.

The hash of hashes looks like this:

%line_sec = (
one => (
WXYZ => ["ABCD", "EFGH"],
TUVW => ["TEFH", "LPCD"],
),
two => (
DVDC => ["GHIJ", "ELFO"],
LMNO => ["PQRS", "TUVW"],
)
);

That's not how you create a hash of hashes of arrays. The keys for %line_sec
hold anonymous hashes, so you should use braces not parentheses:

%line_sec = (
one => {
WXYZ => ["ABCD", "EFGH"],
TUVW => ["TEFH", "LPCD"],
},
two => {
DVDC => ["GHIJ", "ELFO"],
LMNO => ["PQRS", "TUVW"],
}
);

Basically I want to cycle through all of the four letter accronyms to
see if they match a current value called $current_acc. The numbers
"one" "two" etc are command line arguments. The $head_acc variable is
the key of the inner hashes. I have tried the following but it
doesn't work:

foreach $match (@{$line_sec{$ARGV[0]}}{$head_acc})
^^^^

Those braces aren't balanced properly, either:

@{$line_sec{$ARGV[0]}{$head_acc}}


Matt
 
J

Jay Tilton

(e-mail address removed) (cousin_bubba) wrote:

: I am a newbie with perl so please exuse me if this is a dumb question.
:
: In my script I have a hash of hashes and the values of the internal
: hashes are lists or arrays. I need to access all of the elements of
: these arrays for comparison.
:
: The hash of hashes looks like this:
:
: %line_sec = (
: one => (
: WXYZ => ["ABCD", "EFGH"],
: TUVW => ["TEFH", "LPCD"],
: ),
: two => (
: DVDC => ["GHIJ", "ELFO"],
: LMNO => ["PQRS", "TUVW"],
: )
: );

If it really does look like that, you have a train wreck.

More probably, it looks like:

%line_sec = (
one => {
WXYZ => ["ABCD", "EFGH"],
TUVW => ["TEFH", "LPCD"],
},
two => {
DVDC => ["GHIJ", "ELFO"],
LMNO => ["PQRS", "TUVW"],
}
);

: Basically I want to cycle through all of the four letter accronyms

All twelve of them? Or just "WXYZ", "TUVW", "DVDC" and "LMNO" ?

: see if they match a current value called $current_acc. The numbers
: "one" "two" etc are command line arguments. The $head_acc variable is
: the key of the inner hashes. I have tried the following but it
: doesn't work:
:
: foreach $match (@{$line_sec{$ARGV[0]}}{$head_acc})

You did the dereferencing incorrectly.

Compare:
@{ $line_sec{$ARGV[0]} } {$head_acc} # wrong
@{ $line_sec{$ARGV[0]} {$head_acc} } # right

: {
: if ($match eq $current_acc)
: {
: ...
: }
: }

: The array I would like to obtain for the case of "one" and "WXYZ" for
: instance would be (GHIH ELFO).

Do you mean (GHIJ ELFO)?

This is baffling. "one" and "WXYZ" have no evident relationship to the
anonymous array ["GHIJ", "ELFO"] . Are you sure you know what you want?
 
G

gnari

...
The hash of hashes looks like this:
[snipped wrong hash declaration]
Basically I want to cycle through all of the four letter accronyms to
see if they match a current value called $current_acc. The numbers
"one" "two" etc are command line arguments. The $head_acc variable is
the key of the inner hashes. I have tried the following but it
doesn't work:
[snip sort of code]
Is there a way I can do this?

it is not quite clear exactly what values you are looking for, but it looks
to me
like you have your datastructure backwards

you should have the things that you look for as the *keys* of a hash.

if you need to lokup both ways, keep 2 hashes

gnari
 
J

Joe Cipale

Along a similar vein as to the original thread:
I have an cgi-app I am beginning to encode. It is an online fee
calculation page. Users need to be able to add/edit/delete an entry at
wiil, and have
the entries be visible after each operation.

I could use a variant of an online merchant form, but that (appears) to
operate off of a fixed database/storage mechanism. What I am hoping for
is a dynamic hash list (similar to a linked-list in C/C++). Can a hash
of a hash be allocated dynamically?

Regards,

Joe
--
#----------------------------------------------------------#
# "Don't fear the penguin!" #
#----------------------------------------------------------#
# Registered Linux user: #309247 http://counter.li.org #
#----------------------------------------------------------#
 
T

Tad McClellan

Joe Cipale said:
What I am hoping for
is a dynamic hash list (similar to a linked-list in C/C++).


Please do not re-ask FAQs.

perldoc -q linked

How do I handle linked lists?
 
G

gnari

Joe Cipale said:
Along a similar vein as to the original thread:
I have an cgi-app I am beginning to encode. It is an online fee
calculation page. Users need to be able to add/edit/delete an entry at
wiil, and have
the entries be visible after each operation.

I could use a variant of an online merchant form, but that (appears) to
operate off of a fixed database/storage mechanism. What I am hoping for
is a dynamic hash list (similar to a linked-list in C/C++). Can a hash
of a hash be allocated dynamically?

your problem is not clear.
you use word combinations that do not ring any bells
in what way are hashes similar to linked lists?

maybe a more concrete example of what you are tryng to do
would make things clearer.
the comment about storage seems to imply that you need
session information to be carried from request to request,
but you do not want to use storage. is this the case?

I suspect we have a XY problem here. you want to do X,
but think you need Y to do that and ask about Y

if your question is really about linked lists, the read the FAQ,
and then you can ask here it that does not help

gnari
 

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

Similar Threads

hash of hashes 9
process multiple hashes 5
Sorting hash of hashes 3
Sorting hash of hashes 9
Hash of Hashes 5
Word matching with specific parameters 1
ordered hashes 13
having trouble with hash of arrays... 12

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top