CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



steve
Deleted

Feb 26, 2000, 2:26 PM

Post #1 of 4 (707 views)
snippett wanted for word count Can't Post

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

Post #2 of 4 (707 views)
Re: snippett wanted for word count [In reply to] Can't Post

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

Post #3 of 4 (707 views)
Re: snippett wanted for word count [In reply to] Can't Post

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

Post #4 of 4 (707 views)
Re: snippett wanted for word count [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives