Home
TeamSite
How can I get the count of array
Herman
if I have a array(Perl,C,JAVA,Perl,PHP,C)
I want to kow the count of this array? Get rid of the the same!
But not use length(array),
I want to kown how many kind of the array?
Any ideas?
Find more posts tagged with
Comments
Adam Stoller
In Perl, if in scalar context - just use
@array
.
If you're not in scalar context, or aren't sure - force it:
scalar(@array)
I'll leave it to someone else to answer the question regarding other languages.
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
gzevin
..or in perl $#array gives the count as well..... or, to be exact, the index of the last element of an array
Greg Zevin, Ph.D. Comp. Sc.
Independent Interwoven Consultant/Architect
Sydney, AU
Herman
I want to kown perl function,that rignt.
But I say I want to konw how many kind of this
@array(11
,11,22,22,33)
$#array is 5,
but want get 3 (kind of,get rid of the same)
any ideas?
gzevin
ah... first of all you need to get rid of the repetitions in the array. the best way to do it to run it thru a hash (look up the web, heaps of examples.. Perl Cookbook I think has some as well). Then you're crusing....
Greg Zevin, Ph.D. Comp. Sc.
Independent Interwoven Consultant/Architect
Sydney, AU
jbonifaci
If you just want to get the count of unique items:
my
@array
= ('10', '11', '12', '10', '12', '13', '14');
my %hash = map {$_,''}
@array
;
my $hashSize = $#{[keys %hash]};
If you want to remove duplicates from the array:
my
@array
= ('10', '11', '12', '10', '12', '13', '14');
my %hash = map {$_,''}
@array
;
@array
= sort(keys %hash);
~Jeff
Herman
Thank you Jeff, that's just I want!