Re: Finding the number of occurences in an array



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
.



Relevant Pages

  • Re: Range Object Misunderstanding
    ... Public gStrArray() As String ... Public Function rngSrtAs String, ... ' "List" is string array for sorting ... entries from the named range "Groups". ...
    (microsoft.public.excel.programming)
  • Re: string dictionary and memory issue.
    ... ince I am easily loading over 300 000 strings in memory, ... a flag representing the end of a string. ... How many entries are in each node? ... suggest using a plain array, either as a list of entries (for nodes ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about ruby syntax
    ... array of entries containing the html for every tr with a white ... So it takes the page's URI, casts it to a string and replaces the ...
    (comp.lang.ruby)
  • Re: Finding the number of occurences in an array
    ... easy way to find out how many entries in an array match a string? ... the entries in @gfnames contain the string ".txt" without going ...
    (comp.lang.perl.misc)
  • Re: CString Array to LPCTSTR *
    ... of string where he has a diagram with segment descriptors and each of the ... entries in the descriptor table points to segments of string. ... "For vector the element data structure is most likely an array" ...
    (microsoft.public.vc.mfc)