in reply to need help in searching arrayrefs
If you use the word "set" or the word "unique" in the same sentence as the word "array," your design probably needs to be re-thought.
Arrays are slow to search. Hashes are fast to search. Arrays don't help you keep sets of unique things. Hashes help you keep sets of unique things. Arrays are referenced by integer indexes. Hashes are indexed by any kind of string you find convenient to construct.
In fact, you're already using a hash, but you might not realize it. That bit where you say $seen{$_} is using a hash which you never declared. (That could cause problems, not declaring or clearing it beforehand, by the way.)
You look like you're building a concordance or a lexicon. It's all well and good to use this opportunity to learn Perl, and I applaud your desire to solve a problem. You might, however, do a search on google (or perlmonks.org) for the words 'concordance' or 'lexicon' and 'perl'. I expect you'll find quite a few scripts already out there which do the same thing.
use strict; # complain about errors my %lexicon; # collect unique strings while (<>) { # read each line input my @words = # gather list of words map { s/^\W+|\W+$//g; $_ } # strip punctuation split /\s+/; # break line at spaces foreach my $word (@words) { # each word in the line $lexicon{$word}++; # should be counted } } print "$_: $lexicon{$_}\n" # show word and count foreach (sort keys %lexicon); # for all words in order
--
[ e d @ h a l l e y . c c ]
|
|---|