Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 2 Aug 2002 13:58:06 +0200 (MEST)
From:      Rogier Steehouder <r.j.s@gmx.net>
To:        "Christopher J. Umina" <uminac@216-164-225-145.c3-0.wth-ubr2.sbo-wth.ma.cable.rcn.com>
Cc:        FreeBSD Question Mailing List <questions@freebsd.org>
Subject:   Re: checking if something is already in an array
Message-ID:  <12872.1028289486@www34.gmx.net>

next in thread | raw e-mail | index | archive | help
Christopher wrote:
> Hi all,
> 
> 	I'm really stupid lately with Perl and stuff, but I got this list
> of screen names and I made it into an array in perl called @sns.  I want
> to count the amount of times each name shows up in that array and I want
> to make an array of each name in the original array, but only occuring
> once.
> 
> adarc22
> adarc22
> adarc22
<-- etc. -->
> 
> ....is my original array but I want a new array with each name in this
> array appearing only once and count each name's occurance in an additional
> array, even a 2d array works. (if you can do that in Perl...?)
> 
> Thanks a lot,
> Christopher J. Umina

Maybe the following pieces of script will help:

sub uniq {
    my(@list) = @_;
    my(%list);
    map { $list{$_} = 1 } @list;
    return(sort(keys(%list)));
}

or altered for your purpose:

sub uniq {
    my(@list) = @_;
    my(%list);
    map { $list{$_} = 0 } @list;
    map { $list{$_} += 1 } @list;
    return(%list);
}

@list = ('a', 'a', 'a', 'b', 'c', 'c', 'd', 'e', 'e', 'e');
print(@list, "\n");
%list = &uniq(@list);
foreach $item (sort(keys(%list))) {
    print($item, ": ", $list{$item}, "\n");
}

It seems to work for me.

With kind regards, Rogier Steehouder
-- 

-- 
                           ___                          _
-O_\                                                   //
 | /                Rogier Steehouder                 //\
/ \               mailto:r.j.s@gmx.net               //  \
  <----------------------- 90m ---------------------->

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?12872.1028289486>