Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

by Anonymous Monk
on Feb 22, 2008 at 06:14 UTC ( #669470=note: print w/ replies, xml ) Need Help??


in reply to Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

A compacted awk one liner...

echo " one two three four " | awk '{n=split($0,A);S=A[n];{for(i= +n-1;i>0;i--)S=S" "A[i]}}END{print S}'


Comment on Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
Download Code
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
by runrig (Abbot) on Sep 09, 2008 at 16:03 UTC
    Here's another awk solution (different strategy, and slightly shorter when compacted - Update: this reverses every line, and if your's did the same, i.e, without the END block, it would still be shorter than this...oh well):
    #!/bin/awk -f { for (i=1; i<=NF/2; i++) { t=$i $i = $(NF-i+1) $(NF-i+1)=t } print }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://669470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-12-29 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls