how to reverse string in perl?

how to reverse string in perl?

am 30.08.2011 11:38:51 von Narasimha Madineedi

--000e0cd25b2e89bd6304abb5c7c7
Content-Type: text/plain; charset=ISO-8859-1

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......

--
Regards,
Narasimha Madineedi

--000e0cd25b2e89bd6304abb5c7c7--

Re: how to reverse string in perl?

am 30.08.2011 16:58:32 von Shlomi Fish

Hi Narasimha,

On Tue, 30 Aug 2011 15:08:51 +0530
Narasimha Madineedi wrote:

> Hi all,
>=20
> I have a string like *"abcd efgh ijkl mnop"* i want to reverse the string
> like this "*dcba hgfe lkji ponm*"
>=20
> can any one tell me how to get the required output?
> thanks in advance......
>=20

That's not reversing the entire string. That's reversing each word separate=
ly.
It can be done like that (tested):


#!/usr/bin/perl

use strict;
use warnings;

my $s =3D "abcd efgh ijkl mnop";

$s =3D~ s{(\w+)}{scalar reverse($1)}eg;

print "S is now: $s\n";



Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

No selfâ€=90respecting tomboy would use Mandriva.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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

Re: how to reverse string in perl?

am 30.08.2011 17:02:28 von Shawn H Corey

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'

--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

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

Re: how to reverse string in perl?

am 30.08.2011 17:21:34 von Rob Dixon

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;

__END__

**OUTPUT**

dcba hgfe lkji ponm

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

Re: how to reverse string in perl?

am 30.08.2011 17:30:17 von Rob Dixon

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to reverse string in perl?

am 30.08.2011 18:54:57 von Emeka

--0015174763562525f804abbbdf87
Content-Type: text/plain; charset=ISO-8859-1

$s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish

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

print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon

It is time we explain our codes ... I don't think that Narasimha will easily
wrap his head around the first two.

Regards,
Emeka

On Tue, Aug 30, 2011 at 4:30 PM, Rob Dixon wrote:

> 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-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--
*Satajanus Nig. Ltd


*

--0015174763562525f804abbbdf87--

Re: how to reverse string in perl?

am 30.08.2011 19:14:44 von Rob Dixon

On 30/08/2011 17:54, Emeka wrote:
>
>
> $s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish
>
> perl -e'@words=split/\s+/,$ARGV[0];$_=reverse$_
> for@words;print"@words\n";' 'abcd efgh ijkl mnop' Shawn Corey
>
> print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon
>
> It is time we explain our codes ... I don't think that Narasimha will
> easily wrap his head around the first two.

(Once again Emeka, please bottom-post your responses and edit what you
are quoting to what is relevant. Thanks :)

I agree. It is a shame that senior and supposedly wise contributors here
feel the need to boost their egos by posting obscure code. It is
primarily a /beginners/ list, and should be treated as such. If you
cannot teach then please hold off from showing us your wares. Perl
already has a reputation for being incomprehensible without people who
should know better proving that they are right.

In particular I believe Perl should be treated as a programming
language. Its flexibility allows a short script to be passed on the
command line but, unless the question requires it, a perl -e "script"
solution is an ugly way to express a solution. In particular it does not
allow us to insist on the mandatory

use strict;
use warnings;

header, and the consequent declaration of lexical variables.

Rob

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

Re: how to reverse string in perl?

am 30.08.2011 19:38:11 von Shawn H Corey

On 11-08-30 01:14 PM, Rob Dixon wrote:
> On 30/08/2011 17:54, Emeka wrote:
>>
>>
>> $s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish
>>
>> perl -e'@words=split/\s+/,$ARGV[0];$_=reverse$_
>> for@words;print"@words\n";' 'abcd efgh ijkl mnop' Shawn Corey
>>
>> print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon
>>
>> It is time we explain our codes ... I don't think that Narasimha will
>> easily wrap his head around the first two.
>
> (Once again Emeka, please bottom-post your responses and edit what you
> are quoting to what is relevant. Thanks :)
>
> I agree. It is a shame that senior and supposedly wise contributors here
> feel the need to boost their egos by posting obscure code. It is
> primarily a /beginners/ list, and should be treated as such. If you
> cannot teach then please hold off from showing us your wares. Perl
> already has a reputation for being incomprehensible without people who
> should know better proving that they are right.
>
> In particular I believe Perl should be treated as a programming
> language. Its flexibility allows a short script to be passed on the
> command line but, unless the question requires it, a perl -e "script"
> solution is an ugly way to express a solution. In particular it does not
> allow us to insist on the mandatory
>
> use strict;
> use warnings;
>
> header, and the consequent declaration of lexical variables.
>
> Rob
>

OK:

#!/usr/bin/env perl

# prevent silly mistakes
use strict;

# issue warnings of possible mistakes
use warnings;

# read from Perl's data file handle, contents appear after __DATA__
while( my $line = ){

# remove trailing newline
# a newline is whatever is in $/, the $INPUT_RECORD_SEPARATOR
# see `perldoc perlvar` and search for /\$INPUT_RECORD_SEPARATOR/
chomp $line;

# split on whitespace, ignore leading and trailing empty elements
# see `perldoc -f split`
my @words = split ' ', $line;

# do each word, one at a time
foreach my $word ( @words ){

# reverse each word w/ Perl's reverse(). See `perldoc -f reverse`
# since this is scalar context, the string is reversed
# since $word is the `for` iteration variable,
# any changes to it are stored back in @words
$word = reverse $word;

} # end foreach $word

# use Perl's stringification to join the words back into a line
# and print the original line and the modified one
print "$line : @words\n";

} # end while


# The lines after the __DATA__ or __END__
# are read by the file handle
__DATA__
abcd efgh ijkl mnop



--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

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

Re: how to reverse string in perl?

am 30.08.2011 20:07:52 von Emeka

--001517402bfaef7f3004abbce363
Content-Type: text/plain; charset=ISO-8859-1

$s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish

perl -e'@words=split/\s+/,$ARGV[0];
>
> $_=reverse$_
> for@words;print"@words\n";' 'abcd efgh ijkl mnop' Shawn Corey
>
> print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon
>
> It is time we explain our codes ... I don't think that Narasimha will
> easily wrap his head around the first two.
>

(Once again Emeka, please bottom-post your responses and edit what you
are quoting to what is relevant. Thanks :)

I agree. It is a shame that senior and supposedly wise contributors here
feel the need to boost their egos by posting obscure code. It is
primarily a /beginners/ list, and should be treated as such. If you
cannot teach then please hold off from showing us your wares. Perl
already has a reputation for being incomprehensible without people who
should know better proving that they are right.

In particular I believe Perl should be treated as a programming
language. Its flexibility allows a short script to be passed on the
command line but, unless the question requires it, a perl -e "script"
solution is an ugly way to express a solution. In particular it does not
allow us to insist on the mandatory

use strict;
use warnings;

header, and the consequent declaration of lexical variables.

Rob


OK:

#!/usr/bin/env perl

# prevent silly mistakes
use strict;

# issue warnings of possible mistakes
use warnings;

# read from Perl's data file handle, contents appear after __DATA__
while( my $line = ){

# remove trailing newline
# a newline is whatever is in $/, the $INPUT_RECORD_SEPARATOR
# see `perldoc perlvar` and search for /\$INPUT_RECORD_SEPARATOR/
chomp $line;

# split on whitespace, ignore leading and trailing empty elements
# see `perldoc -f split`
my @words = split ' ', $line;

# do each word, one at a time
foreach my $word ( @words ){

# reverse each word w/ Perl's reverse(). See `perldoc -f reverse`
# since this is scalar context, the string is reversed
# since $word is the `for` iteration variable,
# any changes to it are stored back in @words
$word = reverse $word;

} # end foreach $word

# use Perl's stringification to join the words back into a line
# and print the original line and the modified one
print "$line : @words\n";

} # end while

Awesome! Great Job! Thanks a million :(

Regards,
Emeka

On Tue, Aug 30, 2011 at 6:38 PM, Shawn H Corey wrote:

> On 11-08-30 01:14 PM, Rob Dixon wrote:
>
>> On 30/08/2011 17:54, Emeka wrote:
>>
>>>
>>>
>>> $s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish
>>>
>>> perl -e'@words=split/\s+/,$ARGV[0];**$_=reverse$_
>>> for@words;print"@words\n";' 'abcd efgh ijkl mnop' Shawn Corey
>>>
>>> print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon
>>>
>>> It is time we explain our codes ... I don't think that Narasimha will
>>> easily wrap his head around the first two.
>>>
>>
>> (Once again Emeka, please bottom-post your responses and edit what you
>> are quoting to what is relevant. Thanks :)
>>
>> I agree. It is a shame that senior and supposedly wise contributors here
>> feel the need to boost their egos by posting obscure code. It is
>> primarily a /beginners/ list, and should be treated as such. If you
>> cannot teach then please hold off from showing us your wares. Perl
>> already has a reputation for being incomprehensible without people who
>> should know better proving that they are right.
>>
>> In particular I believe Perl should be treated as a programming
>> language. Its flexibility allows a short script to be passed on the
>> command line but, unless the question requires it, a perl -e "script"
>> solution is an ugly way to express a solution. In particular it does not
>> allow us to insist on the mandatory
>>
>> use strict;
>> use warnings;
>>
>> header, and the consequent declaration of lexical variables.
>>
>> Rob
>>
>>
> OK:
>
> #!/usr/bin/env perl
>
> # prevent silly mistakes
> use strict;
>
> # issue warnings of possible mistakes
> use warnings;
>
> # read from Perl's data file handle, contents appear after __DATA__
> while( my $line = ){
>
> # remove trailing newline
> # a newline is whatever is in $/, the $INPUT_RECORD_SEPARATOR
> # see `perldoc perlvar` and search for /\$INPUT_RECORD_SEPARATOR/
> chomp $line;
>
> # split on whitespace, ignore leading and trailing empty elements
> # see `perldoc -f split`
> my @words = split ' ', $line;
>
> # do each word, one at a time
> foreach my $word ( @words ){
>
> # reverse each word w/ Perl's reverse(). See `perldoc -f reverse`
> # since this is scalar context, the string is reversed
> # since $word is the `for` iteration variable,
> # any changes to it are stored back in @words
> $word = reverse $word;
>
> } # end foreach $word
>
> # use Perl's stringification to join the words back into a line
> # and print the original line and the modified one
> print "$line : @words\n";
>
> } # end while
>
>
> # The lines after the __DATA__ or __END__
> # are read by the file handle
> __DATA__
>
> abcd efgh ijkl mnop
>
>
>
> --
> Just my 0.00000002 million dollars worth,
> Shawn
>
> Confusion is the first step of understanding.
>
> Programming is as much about organization and communication
> as it is about coding.
>
> The secret to great software: Fail early & often.
>
> Eliminate software piracy: use only FLOSS.
>
> "Make something worthwhile." -- Dear Hunter
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--
*Satajanus Nig. Ltd


*

--001517402bfaef7f3004abbce363--

Re: how to reverse string in perl?

am 30.08.2011 20:50:19 von Shlomi Fish

Hi Rob,

On Tue, 30 Aug 2011 16:21:34 +0100
Rob Dixon 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 stri=
ng
> > like this "*dcba hgfe lkji ponm*"
> >
> > can any one tell me how to get the required output?
> > thanks in advance......
>=20
> Like this perhaps?
>=20
> use strict;
> use warnings;
>=20
> my $str =3D "abcd efgh ijkl mnop";
>=20
> print join ' ', map scalar reverse($_), split ' ', $str;
>=20

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 examp=
le
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 he=
re,
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__
>=20
> **OUTPUT**
>=20
> dcba hgfe lkji ponm
>=20



--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Parody of "The Fountainhead" - http://shlom.in/towtf

mplayer 0.9.999.2010.03.11-rc5-adc83b19e793491b1c6ea0fd8b46cd9f32e59 2fc now
available for download.
â€=94 Shlomi Fish and d3x.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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

Re: how to reverse string in perl?

am 30.08.2011 21:10:12 von Shawn H Corey

On 11-08-30 02:50 PM, Shlomi Fish wrote:
> 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.

The OP did not specify if a word was only letters, letters and digits,
or anything separated by white-space. As such, one variation is just as
good as the other. Hopefully, enough variations will be presented so
s/he can choose the correct one. :)


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

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