in reply to Re: addup function?
in thread addup function?

eval (join "+", @_) is pretty good.

I'm looking for a way to say it without using the middle-man $total variable.

Is there a way to say it without resorting to an eval?

Ideally I want a builtin that takes a list and an operator and does an eval (join "operator", @list) to produce a scalar.

It's looking like there isn't one.

Replies are listed 'Best First'.
Re: Re: Re: addup function?
by archon (Monk) on Jul 19, 2001 at 01:07 UTC
    the eval answer was a joke. it is far from pretty good and could get you in trouble with evil data.

    there is no builtin that does exactly what you want to do.

(ar0n: reduce) Re (3): addup function?
by ar0n (Priest) on Jul 19, 2001 at 02:00 UTC
    Even though it's not a builtin function, List::Util's reduce function will do most of what you ask:
    my @list = (1, 2, 3, 4); my $sum = reduce { $a + $b } @list; # 10 my $mul = reduce { $a * $b } @list; # 24 my $min = reduce { $a - $b } @list; # -8 my $mod = reduce { $a % $b } @list; # 1 # etc
    ar0n ]

      reduce! I love it. That is exactly how I wanted to say it.

      Thanks you.