Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by toma (Vicar)
on Dec 19, 2006 at 08:07 UTC ( #590620=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)

My awk is rusty!

{ for (i=NF ; i > 0 ; i--) { printf "%s ", $i } }
It should work perfectly the first time! - toma


Comment on Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
Download Code
Replies are listed 'Best First'.
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
by eyepopslikeamosquito (Chancellor) on Dec 21, 2006 at 02:17 UTC

    My awk is very rusty too. Unfortunately, your version emits a trailing space, which violates the spec. I've remedied that below and done it in the form of a function so that it can be more easily compared with the other languages.

    # File : revwords.awk # Run with : awk -f revwords.awk </dev/null END { print reverseWords(" one two three four ") } function reverseWords(s) { if ((i = split(s, words)) == 0) return "" sret = words[i] while (--i > 0) sret = sret " " words[i] return sret }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-07-30 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls