Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 ( [id://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 (Archbishop) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found