substitute all spaces in a sentence except spaces in the first three words
Hi, Suppose I have: my $sentence = 'the quick brown fox jumps over the lazy dog. Now I want it to become: 'the quick brown:fox:jumps:over:the lazy dog. That is, to replace the spaces between brown-fox, fox-jumps, jumps-over, over-the, with :. Should I split the sentence and put each in an array then iterate over the array? What do you think will be the quickest way to do this? Thanks! Now that's room service! Choose from over 150,000 hotels in 45,000 destinations on Yahoo! Travel to find your fit. http://farechase.yahoo.com/promo-generic-14795097
Re: substitute all spaces in a sentence except spaces in the first three words
Michael Alipio wrote: Suppose I have: my $sentence = 'the quick brown fox jumps over the lazy dog. Now I want it to become: 'the quick brown:fox:jumps:over:the lazy dog. That is, to replace the spaces between brown-fox, fox-jumps, jumps-over, over-the, with :. Should I split the sentence and put each in an array then iterate over the array? What do you think will be the quickest way to do this? Your title says one thing and your example says another. You have replaced all spaces with a colon except between the first three and the last three words. What is it you actually want? The code below may help: it strips leading and trailing whitespace and replaces the space between the third through the seventh words with a single colon; all other whitespace is replaced with a single space. If you need anything different from this then please come back to us. HTH, Rob use strict; use warnings; my $sentence = 'the quick brown fox jumps over the lazy dog.'; my @words = split ' ', $sentence; my @centre = splice @words, 2, 5; splice @words, 2, 0, join ':', @centre; $sentence = @words\n; print $sentence, \n; **OUTPUT** the quick brown:fox:jumps:over:the lazy dog. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: substitute all spaces in a sentence except spaces in the first three words
Michael Alipio wrote: Hi, Hello, Suppose I have: my $sentence = 'the quick brown fox jumps over the lazy dog. Now I want it to become: 'the quick brown:fox:jumps:over:the lazy dog. One way to do it: $ perl -le' my $sentence = the quick brown fox jumps over the lazy dog.; $sentence =~ s/ +(?=.* +[^ ]+ +)/ ++$x 2 ? : : /eg; print $sentence; ' the quick brown:fox:jumps:over:the lazy dog. John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/