Friday, May 27, 2011

How to reverse a sentence in Perl

Whether it is more efficient or not will be something you can test but
in Perl you could do something along the lines of:

my $reversed = join( " ", reverse( split( / /, $string ) ) );



OR


Perl makes this kind of text manipulation very easy, you can even test
this easily on the shell:

echo "run as fast as you can" | perl -lne 'print join $",reverse split /\W+/'

or:

echo "all your bases are belong to us" | perl -lne '@a=reverse
/\w+/g;print "@a"'


OR


$_ = "My name is Jack";
unshift @_, "$1 " while /(\w+)/g;
print @_;

No comments: