in reply to Count of patterns in a file without knowing pattern in advance
Use a hash, that way you only need to take one pass over the file:
#!/usr/bin/env perl use strict; use warnings; my %terms; for (<DATA>) { chomp; $terms{$_}++; } for my $key (keys %terms) { print "$key appeared $terms{$key}\n"; } __DATA__ one two three one five two ten one
Update: Well it's very pleasing to find that no less a luminary than Brother choroba has arrived at the exact same answer, albeit of course much faster!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Count of patterns in a file without knowing pattern in advance
by cklatsky (Initiate) on Jun 07, 2019 at 18:37 UTC | |
by NetWallah (Canon) on Jun 11, 2019 at 03:06 UTC | |
by roho (Bishop) on Jun 14, 2019 at 13:21 UTC | |
by hippo (Archbishop) on Jun 14, 2019 at 13:41 UTC | |
by haukex (Archbishop) on Jun 14, 2019 at 13:37 UTC |