Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

accessing a hash

0 views
Skip to first unread message

M Garrish

unread,
May 22, 2001, 12:35:13 AM5/22/01
to
I have a series of hashes numbered pg1, pg2, pg3... containing a number of
values that I want to be able to access based on the info a user enters.
Without boring anyone with the minor details, I ultimately produce a list of
numbers that I run through a loop to create entries on an html page based on
the corresponding hash. To get this script to work, I had to create a
foreach loop like the following:

foreach $val (@rel) {

if ($val == 1) {%pg = %pg1}

elsif ($val == 2) {%pg = %pg2}
elsif ($val == 3) {%pg = %pg3}
# etc., etc. and so forth...

Being new to Perl (and more specifically, to using hashes), is there any way
to condense this code so that I don't have to write out a line for every
possible hash?
I've exhausted my resources and am no closer to a solution... Thanks in
advance for any help.

MG


Randal L. Schwartz

unread,
May 22, 2001, 1:42:22 AM5/22/01
to M Garrish
>>>>> "M" == M Garrish <matthew...@sympatico.ca> writes:

M> I have a series of hashes numbered pg1, pg2, pg3... containing a number of
M> values that I want to be able to access based on the info a user enters.

Almost any time you have a series of variable names, you've got the wrong
data structure already.

You probably want an array of hashes. Not a bunch of individual hashes.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----

Michael Ströck

unread,
May 29, 2001, 9:51:30 AM5/29/01
to
"Randal L. Schwartz" <mer...@stonehenge.com> schrieb im Newsbeitrag
news:m1vgmun...@halfdome.holdit.com...

Michael Ströck

unread,
May 29, 2001, 9:56:28 AM5/29/01
to
"Randal L. Schwartz" <mer...@stonehenge.com> schrieb im Newsbeitrag
news:m1vgmun...@halfdome.holdit.com...
> >>>>> "M" == M Garrish <matthew...@sympatico.ca> writes:
>
> M> I have a series of hashes numbered pg1, pg2, pg3... containing a number
of
> M> values that I want to be able to access based on the info a user
enters.
>
> Almost any time you have a series of variable names, you've got the wrong
> data structure already.
>
> You probably want an array of hashes. Not a bunch of individual hashes.

Forget my last post, please :-)


Michael Ströck


0 new messages