LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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



Reply
 
Search this Thread
Old 12-10-2008, 12:22 PM   #1
RavenLX
Member
 
Registered: Oct 2004
Posts: 95

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

Rep: Reputation: 99
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
 
Old 12-10-2008, 01:44 PM   #3
RavenLX
Member
 
Registered: Oct 2004
Posts: 95

Original Poster
Rep: Reputation: 15
Thumbs up

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


Reply

Tags
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 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 06:34 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration