substring first 100 words from a string in perl

substring first 100 words from a string in perl

am 28.07.2011 15:23:34 von Khabza Mkhize

--20cf3005da886c366804a921122b
Content-Type: text/plain; charset=ISO-8859-1

I want to substring words, I might be using wrong terminology. But I tried
the following example the only problem I have it cut word any where it
likes. eg "breathtaking" on my string is only bre.


$string = "This is an awe-inspiring tour to the towering headland
known as Cape Point. Magnificent beaches, breathtaking views, historic and
picturesque coastal ";

$rtioverview = substr ( $string , 0 , 100 );

Reults = "This is an awe-inspiring tour to the towering headland known
as Cape Point. Magnificent beaches, bre";


any one can help to solve this problem please?

--

Developer
Green IT Web
http://www.greenitweb.co.za

--20cf3005da886c366804a921122b--

Re: substring first 100 words from a string in perl

am 28.07.2011 15:36:47 von Rob Coops

--90e6ba308d76dcc08304a92142ff
Content-Type: text/plain; charset=UTF-8

On Thu, Jul 28, 2011 at 3:23 PM, Khabza Mkhize wrote:

> I want to substring words, I might be using wrong terminology. But I tried
> the following example the only problem I have it cut word any where it
> likes. eg "breathtaking" on my string is only bre.
>
>
> $string = "This is an awe-inspiring tour to the towering headland
> known as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> $rtioverview = substr ( $string , 0 , 100 );
>
> Reults = "This is an awe-inspiring tour to the towering headland known
> as Cape Point. Magnificent beaches, bre";
>
>
> any one can help to solve this problem please?
>
> --
>
> Developer
> Green IT Web
> http://www.greenitweb.co.za
>


Using a regular expression can help here...

#!/usr/local/bin/perl

use strict;
use warnings;

my $string = "This is an awe-inspiring tour to the towering headland known
as Cape Point. Magnificent beaches, breathtaking views, historic and
picturesque coastal ";

my $num_words = 5;
if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){$num_words})/) {
print $words;
}

That should do the trick... you can then very simply pick the number of
words you want to return.
Another option is to split the thing into the various words and stick that
into an array:

my @words = split /\W+/, $string;

It depends on what you want to do with it of course... also considder doing
the following:

if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){1..$num_words})/) {
print $words;
}

Which will return you all words from 1 to the total number fo words so if
you input strign does not contain the total 100 or 5 or what ever other
number of words you are looking for at least you get the words that are
found.

--90e6ba308d76dcc08304a92142ff--

Re: substring first 100 words from a string in perl

am 28.07.2011 19:25:58 von timothy adigun

--0022159f045e43b61504a9247591
Content-Type: text/plain; charset=ISO-8859-1

Hi Rob Coops,

" I want to substring words, I might be using wrong terminology. But I tried
the following example the only problem I have it cut word any where it
likes. eg "breathtaking" on my string is only bre."
-- If you count your $string alphabeth by alphabeth from 0 to 100 including
every space then you will see that your "substr" function is right! I
believe that reg exp can also help as mentioned by
khabza,
but you may have issues getting to your preffered destination on the
$string.
Without altering your code so much check the codes below maybe it could
help!

#!/usr/bin/perl -w
use strict;

# this is your string to examine
my $string = "This is an awe-inspiring tour to the towering headland
known as Cape Point. Magnificent beaches, breathtaking views, historic and
picturesque coastal ";

#ask the user to input the last word
#of the string they are looking for
# e.g if the want the following "This is an awe-inspiring tour to"
# the user will enter the word "to"
print "Enter the extends of your string:";
chomp(my $newStr=<>); #remove LF from user input
# $newStr is used as your
preffered destination on $string
$newStr=checkStr($string,$newStr); #subroutrine to check user input

print substr $string,0,(index($string,"$newStr")+length("$newStr"));


sub checkStr{
my ($orig,$search)=@_;
foreach((split/ /,$orig))
{
if($_ eq $search){return $search}
}
}

On Thu, Jul 28, 2011 at 2:36 PM, Rob Coops wrote:

> On Thu, Jul 28, 2011 at 3:23 PM, Khabza Mkhize
> wrote:
>
> > I want to substring words, I might be using wrong terminology. But I
> tried
> > the following example the only problem I have it cut word any where it
> > likes. eg "breathtaking" on my string is only bre.
> >
> >
> > $string = "This is an awe-inspiring tour to the towering headland
> > known as Cape Point. Magnificent beaches, breathtaking views, historic
> and
> > picturesque coastal ";
> >
> > $rtioverview = substr ( $string , 0 , 100 );
> >
> > Reults = "This is an awe-inspiring tour to the towering headland
> known
> > as Cape Point. Magnificent beaches, bre";
> >
> >
> > any one can help to solve this problem please?
> >
> > --
> >
> > Developer
> > Green IT Web
> > http://www.greenitweb.co.za
> >
>
>
> Using a regular expression can help here...
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $string = "This is an awe-inspiring tour to the towering headland known
> as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> my $num_words = 5;
> if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){$num_words})/) {
> print $words;
> }
>
> That should do the trick... you can then very simply pick the number of
> words you want to return.
> Another option is to split the thing into the various words and stick that
> into an array:
>
> my @words = split /\W+/, $string;
>
> It depends on what you want to do with it of course... also considder doing
> the following:
>
> if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){1..$num_words})/) {
> print $words;
> }
>
> Which will return you all words from 1 to the total number fo words so if
> you input strign does not contain the total 100 or 5 or what ever other
> number of words you are looking for at least you get the words that are
> found.
>

--0022159f045e43b61504a9247591--

Re: substring first 100 words from a string in perl

am 28.07.2011 20:07:34 von timothy adigun

--0015175dd3c6122c0b04a9250a52
Content-Type: text/plain; charset=ISO-8859-1

Hello Khabza,
" I want to substring words, I might be using wrong terminology. But I tried
the following example the only problem I have it cut word any where it
likes. eg "breathtaking" on my string is only bre."
-- If you count your $string alphabeth by alphabeth from 0 to 100 including
every space then you will see that your "substr" function is right! I
believe that reg exp can also help as mentioned by Rob
Coops,
but you may have issues getting to your preffered destination on the
$string.
Without altering your code so much check the codes below maybe it could
help!

#!/usr/bin/perl -w
use strict;

# this is your string to examine
my $string = "This is an awe-inspiring tour to the towering headland
known as Cape Point. Magnificent beaches, breathtaking views, historic and
picturesque coastal ";

#ask the user to input the last word
#of the string they are looking for
# e.g if the want the following "This is an awe-inspiring tour to"
# the user will enter the word "to"
print "Enter the extends of your string:";
chomp(my $newStr=<>);
$newStr=checkStr($string,$newStr); #subroutrine to check user input

print substr $string,0,(index($string,"$newStr")+length("$newStr"));


sub checkStr{
my ($orig,$search)=@_;
foreach((split/ /,$orig))
{
if($_ eq $search){return $search}
}
}

Hello Rob Coops,
Am sorry for the mix up!
Thanks
On Thu, Jul 28, 2011 at 6:25 PM, timothy adigun <2teezperl@gmail.com> wrote:

> Hi Rob Coops,
>
>
> " I want to substring words, I might be using wrong terminology. But I
> tried
> the following example the only problem I have it cut word any where it
> likes. eg "breathtaking" on my string is only bre."
> -- If you count your $string alphabeth by alphabeth from 0 to 100
> including every space then you will see that your "substr" function is
> right! I believe that reg exp can also help as mentioned by khabza,
> but you may have issues getting to your preffered destination on the
> $string.
> Without altering your code so much check the codes below maybe it could
> help!
>
> #!/usr/bin/perl -w
> use strict;
>
> # this is your string to examine
>
> my $string = "This is an awe-inspiring tour to the towering headland
> known as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> #ask the user to input the last word
> #of the string they are looking for
> # e.g if the want the following "This is an awe-inspiring tour to"
> # the user will enter the word "to"
> print "Enter the extends of your string:";
> chomp(my $newStr=<>); #remove LF from user input
> # $newStr is used as your
> preffered destination on $string
> $newStr=checkStr($string,$newStr); #subroutrine to check user input
>
> print substr $string,0,(index($string,"$newStr")+length("$newStr"));
>
>
> sub checkStr{
> my ($orig,$search)=@_;
> foreach((split/ /,$orig))
> {
> if($_ eq $search){return $search}
>
> }
> }
>
> On Thu, Jul 28, 2011 at 2:36 PM, Rob Coops wrote:
>
>> On Thu, Jul 28, 2011 at 3:23 PM, Khabza Mkhize
>> wrote:
>>
>> > I want to substring words, I might be using wrong terminology. But I
>> tried
>> > the following example the only problem I have it cut word any where it
>> > likes. eg "breathtaking" on my string is only bre.
>> >
>> >
>> > $string = "This is an awe-inspiring tour to the towering headland
>> > known as Cape Point. Magnificent beaches, breathtaking views, historic
>> and
>> > picturesque coastal ";
>> >
>> > $rtioverview = substr ( $string , 0 , 100 );
>> >
>> > Reults = "This is an awe-inspiring tour to the towering headland
>> known
>> > as Cape Point. Magnificent beaches, bre";
>> >
>> >
>> > any one can help to solve this problem please?
>> >
>> > --
>> >
>> > Developer
>> > Green IT Web
>> > http://www.greenitweb.co.za
>> >
>>
>>
>> Using a regular expression can help here...
>>
>> #!/usr/local/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $string = "This is an awe-inspiring tour to the towering headland known
>> as Cape Point. Magnificent beaches, breathtaking views, historic and
>> picturesque coastal ";
>>
>> my $num_words = 5;
>> if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){$num_words})/) {
>> print $words;
>> }
>>
>> That should do the trick... you can then very simply pick the number of
>> words you want to return.
>> Another option is to split the thing into the various words and stick that
>> into an array:
>>
>> my @words = split /\W+/, $string;
>>
>> It depends on what you want to do with it of course... also considder
>> doing
>> the following:
>>
>> if (my ($words) = $string =~ /((?:\w+(?:\W+|$)){1..$num_words})/) {
>> print $words;
>> }
>>
>> Which will return you all words from 1 to the total number fo words so if
>> you input strign does not contain the total 100 or 5 or what ever other
>> number of words you are looking for at least you get the words that are
>> found.
>>
>
>

--0015175dd3c6122c0b04a9250a52--

Re: substring first 100 words from a string in perl

am 28.07.2011 21:05:36 von Rob Dixon

On 28/07/2011 14:23, Khabza Mkhize wrote:
>
> I want to substring words, I might be using wrong terminology. But I tried
> the following example the only problem I have it cut word any where it
> likes. eg "breathtaking" on my string is only bre.
>
>
> $string = "This is an awe-inspiring tour to the towering headland
> known as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> $rtioverview = substr ( $string , 0 , 100 );
>
> Reults = "This is an awe-inspiring tour to the towering headland known
> as Cape Point. Magnificent beaches, bre";
>
>
> any one can help to solve this problem please?

It depends what you mean by a 'word'. The 'split' operator will split a
string on whitespace, but on that basis there are only twenty words in
your sample string. Take a look at the program below. If it doesn't help
you then please could you give an example showing what result you would
expect from this data?

Cheers,

Rob


use strict;
use warnings;

my $string = "This is an awe-inspiring tour to the towering headland
known as Cape Point. Magnificent beaches, breathtaking views, historic and
picturesque coastal ";

my @words = split ' ', $string;

print scalar @words, " words:\n";

for my $i (0 .. $#words) {
printf "%3d: %s\n", $i, $words[$i];
}

**OUTPUT**

21 words:
0: This
1: is
2: an
3: awe-inspiring
4: tour
5: to
6: the
7: towering
8: headland
9: known
10: as
11: Cape
12: Point.
13: Magnificent
14: beaches,
15: breathtaking
16: views,
17: historic
18: and
19: picturesque
20: coastal

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: substring first 100 words from a string in perl

am 29.07.2011 08:06:40 von timothy adigun

--20cf300120a2c9c2cf04a92f1501
Content-Type: text/plain; charset=ISO-8859-1

Hi Rob,
I get the point you are making here, if you check the subroutine "sub
checkStr{}" you see dat it confirm what you are pointing out. However, I
think the point there is the number of words the programmers wants! [Khabza,
correct me if am wrong].
Since, split function as indicated will remove the space and just count
words then, the string indicated in the original mail will be 21 not 100!
Maybe like you said it all depends on what Khabza means as "word" - a single
alphabeth or a collection of alphabeth to make words!
For me, in context of the original mail I think Khabza wants words but
counted in alphabeths!
lol!

On Thu, Jul 28, 2011 at 8:05 PM, Rob Dixon wrote:

> On 28/07/2011 14:23, Khabza Mkhize wrote:
>
>>
>> I want to substring words, I might be using wrong terminology. But I tried
>> the following example the only problem I have it cut word any where it
>> likes. eg "breathtaking" on my string is only bre.
>>
>>
>> $string = "This is an awe-inspiring tour to the towering headland
>> known as Cape Point. Magnificent beaches, breathtaking views, historic and
>> picturesque coastal ";
>>
>> $rtioverview = substr ( $string , 0 , 100 );
>>
>> Reults = "This is an awe-inspiring tour to the towering headland
>> known
>> as Cape Point. Magnificent beaches, bre";
>>
>>
>> any one can help to solve this problem please?
>>
>
> It depends what you mean by a 'word'. The 'split' operator will split a
> string on whitespace, but on that basis there are only twenty words in
> your sample string. Take a look at the program below. If it doesn't help
> you then please could you give an example showing what result you would
> expect from this data?
>
> Cheers,
>
> Rob
>
>
> use strict;
> use warnings;
>
> my $string = "This is an awe-inspiring tour to the towering headland
>
> known as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> my @words = split ' ', $string;
>
> print scalar @words, " words:\n";
>
> for my $i (0 .. $#words) {
> printf "%3d: %s\n", $i, $words[$i];
> }
>
> **OUTPUT**
>
> 21 words:
> 0: This
> 1: is
> 2: an
> 3: awe-inspiring
> 4: tour
> 5: to
> 6: the
> 7: towering
> 8: headland
> 9: known
> 10: as
> 11: Cape
> 12: Point.
> 13: Magnificent
> 14: beaches,
> 15: breathtaking
> 16: views,
> 17: historic
> 18: and
> 19: picturesque
> 20: coastal
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--20cf300120a2c9c2cf04a92f1501--

RE: substring first 100 words from a string in perl

am 29.07.2011 14:28:49 von Bob McConnell

My first impression was that he wanted the first hundred characters
rounded off to the previous or next full word. It sounded like he wanted
smart line breads at the word boundaries.

Bob McConnell

From: timothy adigun

> I get the point you are making here, if you check the subroutine "sub
> checkStr{}" you see dat it confirm what you are pointing out. However,
I
> think the point there is the number of words the programmers wants!
[Khabza,
> correct me if am wrong].
> Since, split function as indicated will remove the space and just
count
> words then, the string indicated in the original mail will be 21 not
100!
> Maybe like you said it all depends on what Khabza means as "word" - a
single
> alphabeth or a collection of alphabeth to make words!
> For me, in context of the original mail I think Khabza wants words but
> counted in alphabeths!
> lol!
>=20
> On Thu, Jul 28, 2011 at 8:05 PM, Rob Dixon wrote:
>=20
>> On 28/07/2011 14:23, Khabza Mkhize wrote:
>>
>>>
>>> I want to substring words, I might be using wrong terminology. But I
tried
>>> the following example the only problem I have it cut word any where
it
>>> likes. eg "breathtaking" on my string is only bre.
>>>
>>> $string =3D "This is an awe-inspiring tour to the towering
headland
>>> known as Cape Point. Magnificent beaches, breathtaking views,
historic and
>>> picturesque coastal ";
>>>
>>> $rtioverview =3D substr ( $string , 0 , 100 );
>>>
>>> Reults =3D "This is an awe-inspiring tour to the towering
headland
>>> known
>>> as Cape Point. Magnificent beaches, bre";
>>>
>>> any one can help to solve this problem please?
>>
>> It depends what you mean by a 'word'. The 'split' operator will split
a
>> string on whitespace, but on that basis there are only twenty words
in
>> your sample string. Take a look at the program below. If it doesn't
help
>> you then please could you give an example showing what result you
would
>> expect from this data?
>>
>>
>> use strict;
>> use warnings;
>>
>> my $string =3D "This is an awe-inspiring tour to the towering =
headland
>>
>> known as Cape Point. Magnificent beaches, breathtaking views,
historic and
>> picturesque coastal ";
>>
>> my @words =3D split ' ', $string;
>>
>> print scalar @words, " words:\n";
>>
>> for my $i (0 .. $#words) {
>> printf "%3d: %s\n", $i, $words[$i];
>> }
>>
>> **OUTPUT**
>>
>> 21 words:
>> 0: This
>> 1: is
>> 2: an
>> 3: awe-inspiring
>> 4: tour
>> 5: to
>> 6: the
>> 7: towering
>> 8: headland
>> 9: known
>> 10: as
>> 11: Cape
>> 12: Point.
>> 13: Magnificent
>> 14: beaches,
>> 15: breathtaking
>> 16: views,
>> 17: historic
>> 18: and
>> 19: picturesque
>> 20: coastal

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: substring first 100 words from a string in perl

am 01.08.2011 20:14:47 von rvtol+usenet

On 2011-07-28 15:23, Khabza Mkhize wrote:

> I want to substring words, I might be using wrong terminology. But I tried
> the following example the only problem I have it cut word any where it
> likes. eg "breathtaking" on my string is only bre.
>
>
> $string = "This is an awe-inspiring tour to the towering headland
> known as Cape Point. Magnificent beaches, breathtaking views, historic and
> picturesque coastal ";
>
> $rtioverview = substr ( $string , 0 , 100 );
>
> Reults = "This is an awe-inspiring tour to the towering headland known
> as Cape Point. Magnificent beaches, bre";
>
>
> any one can help to solve this problem please?

perl -wle '

my $string = "This is an awe-inspiring tour to the towering"
. " headland known as Cape Point. Magnificent"
. " beaches, breathtaking views, historic and"
. " picturesque coastal ";

my ($rtioverview) = $string =~ /(.{0,100})\b/;

print $rtioverview;
'

prints:

This is an awe-inspiring tour to the towering headland known as Cape
Point. Magnificent beaches,

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: substring first 100 words from a string in perl

am 01.08.2011 20:23:02 von Rob Dixon

On 01/08/2011 19:14, Dr.Ruud wrote:
>
> my ($rtioverview) = $string =~ /(.{0,100})\b/;

That would have to be

my ($rtioverview) = $string =~ /(.{0,99})\S\b/;

to avoid terminating at the start of a non-space sequence.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/