Help - Search - Member List - Calendar
Full Version: text to words and back
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Ing. Branislav Gerzo
Hello all,

I have

$text = '(this) is just really small test.';

in loop I want get all 2-words and 3-words:
2 words:
(this) is
is just
just really
really small
small test.

3 words:
(this) is just
is just really
....and so on

This is not a big deal, 2 words I did with:

while ($text =~ /(?=(S+s+S+))S+/g) {
print $1, "n";
}

(should be written with w; W, but that's no deal)

similar will be 3 words. But I have to do it this way by comparing all
this words against table, and when something is found put a link. So
output should like:

'(this) <a href="/foo/bar.html">is just</a> really small test.'

I already have routine, which searches table and print back if
something found.

This is quite hard, I done it with 2 words, but I think it is not
optimal and I guess it should be more easy to write 2,3..n word
version.

Could anyone help me on this ?

Thanks

Xavier Noria
On Jun 21, 2005, at 18:24, Ing. Branislav Gerzo wrote:

QUOTE
$text = '(this) is just really small test.';

in loop I want get all 2-words and 3-words:

<snip>

What about doing and split first on the whole text and then use array
slices?

-- fxn

thundergnat
Ing. Branislav Gerzo wrote:
QUOTE
Hello all,

I have

$text = '(this) is just really small test.';

in loop I want get all 2-words and 3-words:
2 words:
(this) is
is just
just really
really small
small test.

3 words:
(this) is just
is just really
...and so on

This is not a big deal, 2 words I did with:

while ($text =~ /(?=(S+s+S+))S+/g) {
print $1, "n";
}

(should be written with w; W, but that's no deal)

similar will be 3 words. But I have to do it this way by comparing all
this words against table, and when something is found put a link. So
output should like:

'(this) <a href="/foo/bar.html">is just</a> really small test.'

I already have routine, which searches table and print back if
something found.

This is quite hard, I done it with 2 words, but I think it is not
optimal and I guess it should be more easy to write 2,3..n word
version.

Could anyone help me on this ?

Thanks


Here is something general case. The $space_number variable is
one less than the number of words you want to return.

I.E.
for two word groups, $space_number should be 1,
for four word groups, $space_number should be 3,
etc.

##################################################
my $text = '(this) is just a really small test.';
my $space_number = 0;
while ($text =~ /(S+((s+S+){$space_number}))/g)
{
print "$1n";
pos $text -= length $2;
}
##################################################

Ing. Branislav Gerzo
Xavier Noria [XN], on Tuesday, June 21, 2005 at 18:31 (+0200) thinks
about:

XN> What about doing and split first on the whole text and then use array
XN> slices?

not bad idea, will try to play with that, it is easier for this task
as while regexp loop

--

...m8s, cu l8r, Brano.

[If you cant find a good comic shop, move. Now!]

John W. Krahn
Ing. Branislav Gerzo wrote:
QUOTE
Hello all,

Hello,

QUOTE
I have

$text = '(this) is just really small test.';

in loop I want get all 2-words and 3-words:
2 words:
(this) is
is just
just really
really small
small test.

3 words:
(this) is just
is just really
...and so on

This is not a big deal, 2 words I did with:

while ($text =~ /(?=(S+s+S+))S+/g) {
print $1, "n";
}

(should be written with w; W, but that's no deal)

similar will be 3 words. But I have to do it this way by comparing all
this words against table, and when something is found put a link. So
output should like:

'(this) <a href="/foo/bar.html">is just</a> really small test.'

I already have routine, which searches table and print back if
something found.

This is quite hard, I done it with 2 words, but I think it is not
optimal and I guess it should be more easy to write 2,3..n word
version.

Could anyone help me on this ?

$ perl -e'
my $text = q/(this) is just really small test./;
for my $count ( 1 .. 3 ) {
while ( $text =~ /(?=(?:^|s)(S+(?:s+S+){$count}))/g ) {
print "$1n";
}
}
'
(this) is
is just
just really
really small
small test.
(this) is just
is just really
just really small
really small test.
(this) is just really
is just really small
just really small test.




John
--
use Perl;
program
fulfillment


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.