Hi Rob,
On Tue, 30 Aug 2011 16:21:34 +0100
Rob Dixon <[email protected]> wrote:
> On 30/08/2011 10:38, 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......
>
> Like this perhaps?
>
> use strict;
> use warnings;
>
> my $str = "abcd efgh ijkl mnop";
>
> print join ' ', map scalar reverse($_), split ' ', $str;
>
The problem with the split+map+join approach (that was taken by you and by
Shawn) is that it:
1. Destructive - does not preserve the whitespace between the words exactly.
2. May not handle different separators than whitespace correctly. For example
if you have words separated by hyphens (e.g: "Over-the-top") or different
punctuation or whatever. I'm not sure what should be the exact behaviour here,
but it may not be handled correctly here.
For these reasons, I think that a s///ge (where /g matches all occurences
and /e evaluates the right side as a Perl expression) would be superior.
Regards,
Shlomi Fish
> __END__
>
> **OUTPUT**
>
> dcba hgfe lkji ponm
>
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Parody of "The Fountainhead" - http://shlom.in/towtf
mplayer 0.9.999.2010.03.11-rc5-adc83b19e793491b1c6ea0fd8b46cd9f32e592fc now
available for download.
— Shlomi Fish and d3x.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/