in reply to Golf: Adding up array elements

I assume you mean to shorten the sub!?
sub sum{eval join"+",@_}

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^2: Golf: Adding up array elements
by xdg (Monsignor) on Nov 02, 2005 at 18:16 UTC
    eval join"+",@_

    Of course, that's not safe if @_ includes a text element with a ";" in it.

    But in the spirit of silly shortenings, how about some recursion?

    sub sum { @_ && pop(@_) + sum(@_) }

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Regarding recursion: I also thought about it, but it's longer. Your's can be shortened:

      sub sum {@_&&(pop)+sum(@_)}


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        And that can be shortened even more.
        sub sum{@_&&(pop)+&sum}