Gossamer Forum
Home : General : Perl Programming :

Arrays and hashes

Quote Reply
Arrays and hashes
I figured out how to get a hash of a two-dimensional array -- for example:

%show = (
Flintstones => [[Fred,Wilma],[Barney,Betty],[Bam-Bam,Pebbles]],
Honeymooners => [[Ralph,Alice],[Ed,Trixie]]);

(I've left out quotes.)

If I use the variable $show{'Flintstones'}[2][0], it will return "Bam-Bam."

What I can't figure out is how to get the number of items in each hash. I'd like to, in this example, be able to find out how many couples were on each show, so

??show{'Flintstones'} = 3 and
??show{'Honeymooners'} = 2

Anybody know the syntax for that? I've tried everything I can think of.

Thanks mucho!


------------------
JPD
Quote Reply
Re: Arrays and hashes In reply to
Hi,

How about:

print $#{$show{'Flintstones'}} + 1;

Since @{$show{'Flintstones'}} is the array in question, we can grab the number of elements from $# which is the last position. We have to add 1 because it's indexed from 0.

Cheers,

Alex
Quote Reply
Re: Arrays and hashes In reply to
Thank you Alex!

I had tried the $# structure, but neglected to put the { } around the variable. Maybe I oughta stop programming at 4am! Smile

Thanks again!!!


------------------
JPD