develooper Front page | perl.beginners | Postings from April 2002

Re: Array question

Thread Previous | Thread Next
From:
ERIC Lawson - x52010
Date:
April 1, 2002 10:25
Subject:
Re: Array question
Message ID:
Pine.LNX.4.21.0204011017470.1199-100000@arwen.bioeng.washington.edu
On Mon, 1 Apr 2002, Allison Ogle wrote:

> I don't know how long the list is and eventually in the list some of
> the names will repeat.  I want to put these names in an array but I
> don't want to repeat any names in the array and I want to keep a count
> of how many times the name appears in the list.  Does anyone have any
> ideas or suggestions?  Thanks,

This is based on one of the FAQ's.  @array1 is your array of names. (See
http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html, Data:Arrays, under
the heading "How do I compute the difference..."):

%count = ();
foreach $element (@array1) { $count{$element}++ }

Your array of unique names is now available from the keys of %count; the
number of occurences of each unique name is available as the value of
%count{$name}.

best,
Eric

-- 

James Eric Lawson
Research Publications Editor
National Simulation Resource

eric@bioeng.washington.edu

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Whereof one cannot speak, thereof one must be silent. -- Wittgenstein


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About