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

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Jul 19, 2012.

  1. Guest

    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
    , Jul 19, 2012
    #1
    1. Advertising

  2. writes:
    > 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
    Rainer Weikusat, Jul 19, 2012
    #2
    1. Advertising

  3. Guest

    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!!


    On Thursday, July 19, 2012 12:54:34 PM UTC-7, Rainer Weikusat wrote:
    > writes:
    > &gt; I have this text file with columns (entries seperated by a tab);
    > &gt;
    > &gt; ++++++++++++++++++++++
    > &gt; Alabama John
    > &gt; Alabama Victor
    > &gt; California Raj
    > &gt; California Jason
    > &gt; California Lucy
    > &gt; Michigan Peter
    > &gt; ++++++++++++++++++++++
    > &gt;
    > &gt; Now I want to know how many people are in each state.
    > &gt; The output should be as follows (seperated by tabs).
    > &gt; +++++++++++++++++++++
    > &gt; Alabama 2 1.John
    > &gt; 2.Victor
    > &gt;
    > &gt; California 3 1. Raj
    > &gt; 2. Jason
    > &gt; 3. Lucy
    > &gt;
    > &gt; Michigan 1 1. Peter
    > &gt; ++++++++++++++++++++++
    > &gt;
    > &gt; I am try the following, but I am confused with the hash processing
    > &gt; after splitting each row in to two values.
    >
    > Example which mostly does this
    >
    > ------------------
    > my (%states, $ppl, $n);
    >
    > while (&lt;DATA&gt;)
    > {
    > chomp;
    > ($state, $name) = split (/\t/, $_);
    > push(@{$states{$state}}, $name);
    > }
    >
    > for (sort(keys(%states))) {
    > $ppl = $states{$_};
    > print(&quot;$_ &quot;.scalar(@$ppl), &quot;\n&quot;);
    >
    > $n = 0;
    > print(join(&quot;\n&quot;, map { &quot;\t&quot;.++$n.&quot; $_&quot;; } @$ppl), &quot;\n&quot;);
    > }
    >
    > __DATA__
    > Alabama John
    > Alabama Victor
    > California Raj
    > California Jason
    > California Lucy
    > Michigan Peter
    , Jul 19, 2012
    #3
  4. wrote:
    >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ürgen Exner, Jul 20, 2012
    #4
  5. Jim Gibson Guest

    In article <>,
    <> wrote:

    > 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");

    --
    Jim Gibson
    Jim Gibson, Jul 20, 2012
    #5
  6. Jürgen Exner <> writes:
    > wrote:
    >>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?


    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.
    Rainer Weikusat, Jul 20, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sara rafiee
    Replies:
    3
    Views:
    1,032
    Scott Allen
    Oct 4, 2004
  2. Marco Ippolito
    Replies:
    0
    Views:
    2,552
    Marco Ippolito
    Oct 11, 2004
  3. Replies:
    0
    Views:
    490
  4. Sara rafiee

    Help me in making users/deleting users in active directory

    Sara rafiee, Oct 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    368
    Robert Koritnik
    Oct 4, 2004
  5. Wilmer Hernandez
    Replies:
    4
    Views:
    439
    Wilmer Hernandez
    Aug 20, 2007
Loading...

Share This Page