Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-10-2008, 11:22 AM   #1
Registered: Oct 2004
Posts: 98

Rep: Reputation: 15
Question How do I get number of elements in a hash of arrays?

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 RavenLX; 12-10-2008 at 11:24 AM. Reason: Title should have said hash "of" arrays.
Old 12-10-2008, 11:47 AM   #2
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Try 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;

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

Old 12-10-2008, 12:44 PM   #3
Registered: Oct 2004
Posts: 98

Original Poster
Rep: Reputation: 15
Thumbs up

Thank you. This is exactly what I was looking for.


array, elements, hash, perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:49 PM
Simple perl problem with hash of arrays the theorist Programming 9 07-26-2007 01:25 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 PM.

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