CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Counting Hash

 



JFrost
Novice

Jul 9, 2001, 10:46 AM

Post #1 of 6 (1064 views)
Counting Hash Can't Post

Hi,

I have the following hash table.

%select_fields = (

nationality => 'Jewish,Russian,Ukrainian,Other',
maritalstatus => 'Never Married,Divorced,Widowed,Separated',
children => 'No Children,1,2,3,4,5',
body => 'Small Frame,Thin,Toned,Proportional,Average wight,Broad Build,Portly'
);

How can I count the amount of entries in one of the hashes. i.e.

nationality has 4 entries.

Thanks



(This post was edited by JFrost on Jul 9, 2001, 9:47 AM)


mhx
Enthusiast / Moderator

Jul 9, 2001, 11:36 AM

Post #2 of 6 (1061 views)
Re: Counting Hash [In reply to] Can't Post

Hi,

I have a solution, but I really don't like it (and I hope you don't use it):

Code
my $nationalities = $select_fields{nationality} =~ s/,/,/g + 1;

But I have another solution, which I like a lot better. Why don't you use a hash of arrays? It's easier to use and the elements can be counted quite easily by putting the array in scalar context:

Code
#!/bin/perl -w 
use strict;

my %select_fields = (
nationality => ['Jewish', 'Russian', 'Ukrainian', 'Other'],
maritalstatus => ['Never Married', 'Divorced', 'Widowed', 'Separated'],
children => ['No Children', '1', '2', '3', '4', '5'],
body => ['Small Frame', 'Thin', 'Toned', 'Proportional',
'Average wight', 'Broad Build', 'Portly'],
);

printf "'$_' field count: %d\n", scalar @{$select_fields{$_}}
foreach keys %select_fields;

So, for instance the number of nationalies would simply be

Code
my $nationalities = @{$select_fields{nationality}};

I hope this helps.

-- Marcus



kiseok7
stranger

Jul 10, 2001, 12:08 AM

Post #3 of 6 (1045 views)
Re: Counting Hash [In reply to] Can't Post

$amount{$_} = split /,/, $select_fields{$_} foreach( keys %select_fields );

foreach (keys %amount ) {
print $_,"\t",$amount{$_},"\n";
}


nothing


mhx
Enthusiast / Moderator

Jul 10, 2001, 12:49 AM

Post #4 of 6 (1039 views)
Re: Counting Hash [In reply to] Can't Post

Though this is an obvious solution, unfortunately calling split in scalar context will implicitly split the string into @_. Since this is a deprecated feature, it triggers a warning

Code
Use of implicit split to @_ is deprecated

when you're running under -w.

-- Marcus



Mortimer
journeyman

Jul 10, 2001, 1:49 AM

Post #5 of 6 (1034 views)
Re: Counting Hash [In reply to] Can't Post

I would also use a hash of arrays for storage as Marcus suggested. But if you still feel the need to do it as it stands, you could take the reference and immediately dereference it...


Code
print scalar @{ [ split /,/, $select_fields{ children } ] };

Dave.
www.dmscripts.com
davemortimer@bigpond.com




kiseok7
stranger

Jul 10, 2001, 1:57 AM

Post #6 of 6 (1031 views)
Re: Counting Hash [In reply to] Can't Post

thanks

$amount{$_} = @_ = split /,/, $select_fields{$_} foreach( keys %select_fields );

7;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives