Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by jdporter (Canon)
on Dec 12, 2006 at 15:50 UTC ( #589299=note: print w/ replies, xml ) Need Help??


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

Huh?

" one two three four ".split(/ /).reverse().join(" ")
We're building the house of the future together.


Comment on Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
Download Code
Re^3: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by stvn (Monsignor) on Dec 12, 2006 at 15:56 UTC

    Actually that doesn't work correctly in FF or Safari on OS X. It still has several empty strings filling up the array. They become more evident if you do this:

    " one two three four ".split(/ /).reverse().join("|")
    which gives you "||||four|three|two|||one||".

    -stvn

      Interesting... It works fine in EcmaScript. Or at least Jscript. :-)
      So how about

      " one two three four ".split(/ +/).reverse().join(" ")
      We're building the house of the future together.

        Yup, that one works. Boring, but it works ;)

        -stvn
        Trimming the string is still necessary in order to comply with the specs. Yes, very boring ;-)
        function reverseWords(str) { return str.replace(/^\s+/,'').replace(/\s+$/,'').split(/\s+/).revers +e().join(' '); };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-11-25 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls