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 <rob.di...@gmx.com> 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-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >