Help needed to finding how many users are in a group..

C

clearguy02

Hi all,

I have this text file with columns (entries seperated by a tab);

++++++++++++++++++++++
Alabama John
Alabama Victor
California Raj
California Jason
California Lucy
Michigan Peter
++++++++++++++++++++++

Now I want to know how many people are in each state.
The output should be as follows (seperated by tabs).
+++++++++++++++++++++
Alabama 2 1.John
2.Victor

California 3 1. Raj
2. Jason
3. Lucy

Michigan 1 1. Peter
++++++++++++++++++++++

I am try the following, but I am confused with the hash processing after splitting each row in to two values. Can some one help me out here?

+++++++++++++++++++++
while (<DATA>)
{
($state, $name) = split (\t, $_);
...
....
}

__DATA__
Alabama John
Alabama Victor
California Raj
California Jason
California Lucy
Michigan Peter
 
R

Rainer Weikusat

I have this text file with columns (entries seperated by a tab);

++++++++++++++++++++++
Alabama John
Alabama Victor
California Raj
California Jason
California Lucy
Michigan Peter
++++++++++++++++++++++

Now I want to know how many people are in each state.
The output should be as follows (seperated by tabs).
+++++++++++++++++++++
Alabama 2 1.John
2.Victor

California 3 1. Raj
2. Jason
3. Lucy

Michigan 1 1. Peter
++++++++++++++++++++++

I am try the following, but I am confused with the hash processing
after splitting each row in to two values.

Example which mostly does this

------------------
my (%states, $ppl, $n);

while (<DATA>)
{
chomp;
($state, $name) = split (/\t/, $_);
push(@{$states{$state}}, $name);
}

for (sort(keys(%states))) {
$ppl = $states{$_};
print("$_ ".scalar(@$ppl), "\n");

$n = 0;
print(join("\n", map { "\t".++$n." $_"; } @$ppl), "\n");
}

__DATA__
Alabama John
Alabama Victor
California Raj
California Jason
California Lucy
Michigan Peter
 
C

clearguy02

Thanks Rainer..

That works like a charm. How can I number the states as well?

$l = 0;
print("++$l.$_ ".scalar(@$ppl), "\n"); is not working!!
 
J

Jürgen Exner

Now I want to know how many people are in each state.
The output should be as follows (seperated by tabs).


Does this smell like homework to anyone else, too?

jue
 
J

Jim Gibson

Thanks Rainer..

That works like a charm. How can I number the states as well?

$l = 0;
print("++$l.$_ ".scalar(@$ppl), "\n"); is not working!!

You have to remove ++$l from its double-quoted context and make it an
expression:

print( ++$l . ".$_ ".scalar(@$ppl), "\n");
 
R

Rainer Weikusat

Jürgen Exner said:
Does this smell like homework to anyone else, too?

Maybe it does. But it is IMHO much better to show people how to solve
simple problems like that than to force them to rediscover all
well-known solutions which have piled up since the times of Aristotle.
Geniuses are rare and surreptitious(!) for day-to-day
operations. Trained craftsmen are useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top