Re: Algorithm help for unique string searching/counting within an array.



Paul Van Delst <Paul.vanDelst@xxxxxxxx> wrote:

I want to be able to search an array of strings (with many repeated elements) so I can
count the unique elements.

Are you sure you want to use Fortran for this? Languages like Perl or
awk have built-in arrays with strings as subscripts. Perl calls
them "hashes".

Here's a short Perl program (for illustration), which assumes that
all the strings are in separate lines.

#! /usr/bin/perl

while(<>) { # Loop over all entries in the array, putting values into $_ .
chomp; # Eat newlines
$string{$_}++; # Increment the hash element with the subscript $_ by
# one, creating the element if necessary.
}

print scalar(keys(%string)),"\n"; # Count the number of keys to the hash.

Maybe somebody has implemented hash tables in Fortran as a module
that you could reuse.
.



Relevant Pages

  • Re: Idiom for array index that Im foreaching over?
    ... >>and have an array or hash of those (not sure what the hash key would ... you can write Fortran in any language. ... The hash above is exactly what one should do in perl. ...
    (comp.lang.perl.misc)
  • Re: Noob! Help required and would be really appreciated!
    ... You've read 60% of "Learning Perl"? ... On finding the keyword "New Order", it has to start building a hash. ... want to build a loop around this hash creation. ... transferred to %allOrders. ...
    (comp.lang.perl.misc)
  • Re: Passing an array to a sub routine
    ... but later you're using a hash. ... In Perl, these are very ... The LoadDataFile routine works correctly, ...
    (comp.lang.perl.misc)
  • Re: convert f77 code from mixed case to lower case
    ... If you like, you can offer a Fortran program that does the job, and I will ... offer a Perl script. ... language being better than the other for the task. ... quite sure I could become fluent in Perl, ...
    (comp.lang.fortran)
  • Making Datatypes Constant and Emulating Const Correctness
    ... Being relatively new to Perl with Java and bits of C under my belt, ... I'm new to C+++, too, but remember a number of uses for the const keyword ... an array to some analizer function as a constant strongly signals that no ... some reference (e,g. a blessed hash reference) ...
    (comp.lang.perl.misc)