in reply to addup function?

well you could use
map {$total += $_} @_;
or
$total += eval (join "+", @_)

or any other number of abominations... but what's wrong with the way you're doing it now?

Replies are listed 'Best First'.
Re: Re: addup function?
by tomazos (Deacon) on Jul 19, 2001 at 00:55 UTC
    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.

      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.

      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.