On Jan 25, Dave Rankin said:
>be a bad approach. Here's the code I came up with:
The "best" approach is to use a hash, since it is optimized for making
sure its keys are unique.
>my $matches; ## running with "use strict;"
>while (<>) {
> while (/(\w+)/ig) {
The /i isn't needed, since \w matches upper- and lower-case letters.
> $matches!~ /$1/i ? $matches.="$1 " : next;
By using a regex here, /$1/i, you end up clearing $1 afterwards (since
there are no parentheses in the regex) so that $matches .= "$1 " ends up
appending just " " to $matches, since $1 is empty.
Also, your method comes up with false matches in the input "bite it"; it
matches "bite" and puts that in $matches, but when it matches "it", it
sees that $matches has "it" in it.
I would suggest, if you really wanted this approach:
while (<>) {
while (/(\w+)/g) {
my $word = $1;
$matches .= "$word " if $matches !~ /$word/;
}
}
> }
>}
>$unique_words=()=$matches=~ /\s/g;
This can be done faster as:
$unique_words = $matches =~ tr/ //;
That counts the number of spaces in $matches.
This being said, a hash is undoubtedly a better approach.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]