Problem with hashes

C

cousin_bubba

I have a script that contains several hashes. I need to retrieve
information from only one of the hashes depending on what argument the
user passes to the script. To access the values in the hashes I
would like to do the following:

@{$line_xx_section{$variable}

in this case the hash name is $line_xx_section and the "xx" part is
the argument passed by the user. I tried
@{$line_$ARGV[0]_section{$variable}} but it won't run. Any idea how I
can make this work.

Thanks for your help and putting up with my newbie problems.
 
A

Anno Siegel

cousin_bubba said:
I have a script that contains several hashes. I need to retrieve
information from only one of the hashes depending on what argument the
user passes to the script.

Then use a hash of hashes (HOH). As keys of the outer hash use the possible
user inputs, the structure of the inner hashes stays what it is now.
To access the values in the hashes I
would like to do the following:

@{$line_xx_section{$variable}
Huh?

in this case the hash name is $line_xx_section and the "xx" part is
the argument passed by the user. I tried
@{$line_$ARGV[0]_section{$variable}} but it won't run. Any idea how I
can make this work.

That's a futile attempt at symbolic references. Don't go that way.
With a HOH, you access the selected hash through

my $current_subhash = $hoh{ $ARGV[ 0]};

Anno
 
T

Tad McClellan

cousin_bubba said:
I
would like to do the following:

@{$line_xx_section{$variable}


What do the unbalanced curlie brackets mean?

in this case the hash name is $line_xx_section


No it isn't.

If it starts with a dollar sign then it is the name of a scalar.

The name of the hash in question is %line_xx_section.

$line_xx_section{$variable} accesses a scalar _value_
in the hash, not the whole hash structure.

The scalar named $line_xx_section does not appear anywhere
in the code you posted, and if it did, it would not have
any relationship with a hash named line_xx_section other
than similar-looking names.

Any idea how I
can make this work.


Use a hash-of-hashes (HoH).

perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc
 

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

Iterating hashes 11
process multiple hashes 5
Cracking hashes with Python 22
hash of hashes 9
Some sort questions - especially hashes 4
Quick sort algorithm 1
Parsing an Array of Hashes 3
Hashes 4

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top