Re: Golf: Adding up array elements
by Limbic~Region (Chancellor) on Nov 02, 2005 at 17:23 UTC
|
say [+] @_;
| [reply] [d/l] |
Re: Golf: Adding up array elements
by Roy Johnson (Monsignor) on Nov 02, 2005 at 16:48 UTC
|
| [reply] [d/l] |
Re: Golf: Adding up array elements
by Skeeve (Parson) on Nov 02, 2005 at 16:48 UTC
|
I assume you mean to shorten the sub!?
sub sum{eval join"+",@_}
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
sub sum {@_&&(pop)+sum(@_)}
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
|
Re: Golf: Adding up array elements
by ioannis (Abbot) on Nov 02, 2005 at 16:57 UTC
|
Besides brevity, it might also execute faster since Util.xs
informs us that sum() is coded in C, as NV sum(...)
use List::Util qw(sum);
print sum (my @arr = qw( 1 3 5 6));
| [reply] [d/l] |
Re: Golf: Adding up array elements
by jeffa (Bishop) on Nov 02, 2005 at 16:42 UTC
|
I was never very good at Golf, but here goes:
perl -le'@_=1..5;$s+=$_ for@_;print$s'
| [reply] [d/l] |
Re: Golf: Adding up array elements
by Perl Mouse (Chaplain) on Nov 02, 2005 at 16:48 UTC
|
perl -le'@a=(1,3,5,6);$"="+";print eval"@a"'
perl -le'@a=(1,3,5,6);$,="+";print@a'|bc
| [reply] [d/l] |
Re: Golf: Adding up array elements
by ambrus (Abbot) on Nov 03, 2005 at 09:02 UTC
|
This is not exactly golf, and works only for nonnegative integers.
#!perl
use warnings;
use strict;
{
my $adder = '
G
s/^ *\([0-9]*\) *\n\([0-9]*\)$/0\1pm0\2nb0/;td;d;
:d;s/n\(\(b\).*0\|\)[^0]*$/nb98765432109876543210a0123456789\2
+/;
s/\(.\)\(p.*\)\(.\)\(n.*\1\).*a.*\3/\2\4\3/;
s/^0*\(.*\)p\(.*m\)0*\(.*\)\(.\).\{11\}$/0\1p\4\20\3/;
/m0*n.*0/!bd;
s/^\(.*\)p\(.*\)m.*/\1\2/;s/^0*//;
h;$!d
';
$adder =~ s/;/\n/g; # some seds don't like semicolons
sub printsum {
open my $SED, "|-", "sed", $adder or die "error running sed: $
+!";
print $SED int($_), "\n" or die "error writing to sed: $!"
for @_;
close $SED or die "error closing sed: $!";
}
}
my @array = (1,3,5,6);
printsum(@array);
__END__
Update 2007-12-06: see Re^2: --- adding a column of integers
Update 2011-10-29: see also (almost) foldl. | [reply] [d/l] |
|
Not golf at all, but an excellent obfuscation! :)
| [reply] |
Re: Golf: Adding up array elements
by Aristotle (Chancellor) on Nov 02, 2005 at 21:20 UTC
|
sub sum { my$t;$t+=$_ for@_;$t }
The eval approaches are shorter, but unsafe. It’s your call whether that’s acceptable. For real code, it’s not. But in real code I’d use List::Util.
Update: added space as per sauoq.
Makeshifts last the longest. | [reply] [d/l] |
|
| [reply] [d/l] |