Home: Perl Programming Help: Intermediate:
snippett wanted for word count



steve
Deleted

Feb 26, 2000, 2:26 PM


Views: 1962
snippett wanted for word count

Hello,

I am looking for the best way to return X number of words to a variable...

$short = First 30 words of $long.

I think I can do this by just going letter by letter counting the spaces and then stopping at a 30 spaces. But it seems like there might be a faster method.

Thanks,
Steve


steve
Deleted

Feb 26, 2000, 10:17 PM


Views: 1962
Re: snippett wanted for word count

Thanks a lot, I will play with that.

I do have one question though, what does the "\" do in the following line?
my \$i = 0;

Thanks again,
Steve


japhy
Enthusiast

Feb 27, 2000, 6:12 AM


Views: 1962
Re: snippett wanted for word count

It depends on what you call a "word". Here's something that you can use:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# get_words EXPR, WCHARS, MAXWORDS

sub get_words {
my ($string, $wchr, $max) = @_;
$max &#0124; &#0124;= 0;
my @words;

eval << "END OF REGEX";
local \$_ = \$string;
for (
my \$i = 0;
($max < 1 or \$i < $max) and /\\G([$wchr]+[^$wchr]*)/g;
\$i++) {
push \@words, \$1;
}
END OF REGEX

return wantarray ? @words : join "", @words;
}
</pre><HR></BLOCKQUOTE>

It's used as follows:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$text = "What's up with you?";
$short = get_words($text, "a-zA-Z", 3);
# What's up
$short = get_words($text, "-'a-zA-Z", 3);
# What's up with
@chunks = get_words($text, "a-zA-Z",
</pre><HR></BLOCKQUOTE>

The second argument is the character class that "words" are made up of. If the third argument is not passed, it will break the string up as many times as needed. Play with the function a bit.

[This message has been edited by japhy (edited 02-27-2000).]


japhy
Enthusiast

Feb 27, 2000, 11:14 AM


Views: 1962
Re: snippett wanted for word count

I had to put a \ before a couple of the variables ($i and @words) because I was creating code in a double quoted string. If you want to see what I mean, then do this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub get_words {
my ($string, $wchr, $max) = @_;
$max &#0124; &#0124;= 0;
my @words;
my $code = << "END OF REGEX";
local \$_ = \$string;
for (
my \$i = 0;
($max < 1 or \$i < $max) and /\\G([$wchr]+[^$wchr]*)/g;
\$i++) {
push \@words, \$1;
}
END OF REGEX

print "THE CODE IS:\n$code\n";
eval $code;

return wantarray ? @words : join "", @words;
}
</pre><HR></BLOCKQUOTE>

That will show you what the code I constructed looks like, and THEN I evaluate it as actual Perl code.