There's a Perl technique I often find myself using for finding unique strings in an array that involves using a hash to keep track of which strings have already been processed. It goes something like this:
What would be the nearest C equivalent to that?Code:%seen = (); foreach $string (@list) { next if $seen($string) == 1; # do some stuff with $string $seen($string) = 1; }
The context is that I have some code written by someone else that loads a large number of entries into a database, but needs to check that they have not already been loaded. It does this by repeatedly querying the database and this becomes very slow. Using the above Perl trick avoids this problem but a Perl script to do the same as the C code is not very efficient otherwise.