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 ambrus (Abbot)
on Sep 09, 2008 at 15:46 UTC ( #710122=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)

This thread has no sed solution yet?

The following program reads a single line, and gives its words in reverse.

sed 's/^/@/;:;s/\(.*@\)[ \t]*\([^ \t][^ \t]*\)/\2 \1/;t;s/ *@.*//;q'

(This works in GNU sed. If you use another sed, you may need to change the semicolons to new lines, and the \t escapes to real tab characters.)

This really breaks if the input contains an at-sign. If you need such inputs, change the at-sign to some other character that doesn't occurr in the input, or use the following more complicated snippet.

sed 's/@/@a/g;s/^/@s/;:;s/\(.*@s\)[ \t]*\([^ \t][^ \t]*\)/\2 \1/;t;s/ +*@s.*//;s/@a/@/g;q'


Comment on Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-09-23 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls