Re: Finding the number of occurences in an array
- From: Josef Möllers <1700-820@xxxxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 11:04:38 +0200
Bill H wrote:
Perl has so many quick ways of doing things, I wonder if there is an
easy way to find out how many entries in an array match a string?
if I use the following to get a directory list of the sub directory
"things" into @gfnames, is there an easy way of find out how many of
the entries in @gfnames contain the string ".txt" without going through
and comparing each one?
Well, simply by magic!
You use the magic wand to transform the array into a count! Entries without
the .txt extension will not be considered. Also, since you already know
which entries have the .txt extension, you just need to _count_ the others,
no comparison needed! That way, you could even sort an arbitrary large
array in constant time!
Seriously: how on earth do you think you can determine whether an item
belongs into a given class without looking at the item?
You can usually skip the first and second entry, as they are '.' and '..',
but you still have to check the rest one by one.
You could offload that out of your code by doing:
foreach (<things/*.txt>) {
push @gfnames, $_;
}
print "There are ", scalar @gfnames, " .txt-Files\n";
But that would still check each and every entry elsewhere.
--
josef punkt moellers bei gmx punkt de
.
- References:
- Finding the number of occurences in an array
- From: Bill H
- Finding the number of occurences in an array
- Prev by Date: Re: Find duplicates in a dat file
- Next by Date: Re: sort and keep the latest version
- Previous by thread: Re: Finding the number of occurences in an array
- Next by thread: Re: Finding the number of occurences in an array
- Index(es):
Relevant Pages
|