From owner-freebsd-questions Fri Aug 2 4:59:55 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7526C37B405 for ; Fri, 2 Aug 2002 04:59:41 -0700 (PDT) Received: from mx0.gmx.net (mx0.gmx.net [213.165.64.100]) by mx1.FreeBSD.org (Postfix) with SMTP id 911CC43E42 for ; Fri, 2 Aug 2002 04:58:28 -0700 (PDT) (envelope-from r.j.s@gmx.net) Received: (qmail 10502 invoked by uid 0); 2 Aug 2002 11:58:06 -0000 Date: Fri, 2 Aug 2002 13:58:06 +0200 (MEST) From: Rogier Steehouder To: "Christopher J. Umina" Cc: FreeBSD Question Mailing List MIME-Version: 1.0 Subject: Re: checking if something is already in an array X-Priority: 3 (Normal) X-Authenticated-Sender: #0008145134@gmx.net X-Authenticated-IP: [130.89.226.219] Message-ID: <12872.1028289486@www34.gmx.net> X-Mailer: WWW-Mail 1.5 (Global Message Exchange) X-Flags: 0001 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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