On 30/08/2011 16:02, Shawn H Corey wrote:
On 11-08-30 05:38 AM, Narasimha Madineedi wrote:
Hi all,

I have a string like *"abcd efgh ijkl mnop"* i want to reverse the
string like this "*dcba hgfe lkji ponm*"

can any one tell me how to get the required output? thanks in
advance......


perl -e'@words=split/\s+/,$ARGV[0];$_=reverse$_
for@words;print"@words\n";' 'abcd efgh ijkl mnop'

Please remember that the default parameters for 'split' are almost
always what is required. Splitting on /\s+/ is almost the same, but will
return an empty initial field if the object string has leading
whitespace. perldoc -f split says this:

As a special case, specifying a PATTERN of space (' ') will split on
white space just as "split" with no arguments does. ... A "split" on
"/\s+/" is like a "split(' ')" except that any leading whitespace
produces a null first field. A "split" with no arguments really does
a "split(' ', $_)" internally.

So to extract all non-whitespace substrings from $ARGV[0] you should write

  split ' ', $string;


Rob

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


Reply via email to