Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by shmem (Canon)
on Dec 12, 2006 at 15:09 UTC ( #589293=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)

perl5 version without join -
sub reverseWords { "@{[reverse split' ',shift]}" }

<update>

and without split -

sub reverseWords { "@{[reverse shift=~/\S+/g]}" }

</update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by bart (Canon) on Dec 13, 2006 at 11:12 UTC
    Nice, but you're counting on the special variable $" to have its default value. There's no reason for people not to touch it.

    Either use join, or use

    local $" = ' ';
    in your sub.
      ...yeah, and if I get paranoid I also check whether the stringify and/or (de)ref operators are overloaded, and maybe somebody changed shift and reverse via e.g. schwern's Function::Override... ;-)

      I guess that if somebody changes $" globally, they have a reason, now don't they?

      --shmem

      update: inserted globally

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Humbug. If your function API isn't explicitely saying it's using the current value of $", then it shouldn't use its current value.

        Do not assume people haven't touched it. They have every right to, without having to appologize for it, to you.

        Don't forget code filters, too! :-) Just because it says "sub" doesn't mean it actually *means* sub! ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589293]
help
Chatterbox?
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please
[thepkd]: i used a series of {}'s but it dont work
[choroba]: You need square brackets for arrays
[GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
[thepkd]: to dereference i mean
LanX .oO( oh tempz, oh moretz)
[thepkd]: @GotToBTru Sure. But.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2016-12-06 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.