LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 12-10-2008, 12:22 PM   #1
FuzzieDice
Member
 
Registered: Oct 2004
Location: Rochester, NY
Distribution: Whatever Distro I need to work with.
Posts: 81
Thanked: 0
Question How do I get number of elements in a hash of arrays?


[Log in to get rid of this advertisement]
Ok, I have something like this:

$arr_hash{'produce'}{'veggies'}[0] = Broccoli
$arr_hash{'produce'}{'veggies'}[1] = Cauliflower
$arr_hash{'produce'}{'veggies'}[2] = Carrots

$arr_hash{'produce'}{'color'}[0] = Green
$arr_hash{'produce'}{'color'}[1] = Orange
$arr_hash{'produce'}{'color'}[2] = White

What I want to know is how do you tell how many elements are in $arr_hash{'produce'}{'veggies'} for example. It should be 3. But I can't get anything to work to produce # of elements for that.

For example, I've tried:

print $#arr_hash{'produce'}{'veggies'} . "\n";

And it doesn't work.

Anyone know how to do this? Thanks.

Last edited by FuzzieDice; 12-10-2008 at 12:24 PM.. Reason: Title should have said hash "of" arrays.
FuzzieDice is offline  
Tag This Post , , ,
Reply With Quote
Old 12-10-2008, 12:47 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Newport News, Virginia, USA
Distribution: Fedora, Kubuntu, RedHat, SuSe, Sabayon
Posts: 1,106
Thanked: 40
Try this:

Code:
#!/usr/bin/perl

$arr_hash{'produce'}{'veggies'}[0] = Broccoli;
$arr_hash{'produce'}{'veggies'}[1] = Cauliflower;
$arr_hash{'produce'}{'veggies'}[2] = Carrots;

$arr_hash{'produce'}{'color'}[0] = Green;
$arr_hash{'produce'}{'color'}[1] = Orange;
$arr_hash{'produce'}{'color'}[2] = White;

$aref_veggies =  $arr_hash{'produce'}{'veggies'};
print $#$aref_veggies + 1 . "\n";
print scalar @$aref_veggies . "\n";
HTH

Forrest
forrestt is offline     Reply With Quote
Thanked by:
Old 12-10-2008, 01:44 PM   #3
FuzzieDice
Member
 
Registered: Oct 2004
Location: Rochester, NY
Distribution: Whatever Distro I need to work with.
Posts: 81
Thanked: 0

Original Poster
Thumbs up

Thank you. This is exactly what I was looking for.
FuzzieDice is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 01:49 PM
Simple perl problem with hash of arrays the theorist Programming 9 07-26-2007 02:25 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 06:08 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 04:59 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 09:07 AM


All times are GMT -5. The time now is 12:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration