From: Peng Yu on
Hi,

I use the following command to OR the values in an array. I'm
wondering what is the best way to do so. A function similar to
'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
in perl.

my @array=(0,0,1);
my $result=0;
map { $result = $result || $_} @array;
print $result, "\n";

Regards,
Peng
From: Tad McClellan on
Peng Yu <pengyu.ut(a)gmail.com> wrote:
> Hi,
>
> I use the following command to OR the values in an array. I'm
> wondering what is the best way to do so. A function similar to
> 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
> in perl.
>
> my @array=(0,0,1);
> my $result=0;
> map { $result = $result || $_} @array;


$result ||= $_ for @array; # map in void context is silliness anyway...


> print $result, "\n";


see the "Assignment Operators" section in

perldoc perlop


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
From: Ben Morrow on

Quoth Peng Yu <pengyu.ut(a)gmail.com>:
> Hi,
>
> I use the following command to OR the values in an array. I'm
> wondering what is the best way to do so. A function similar to
> 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
> in perl.
>
> my @array=(0,0,1);
> my $result=0;
> map { $result = $result || $_} @array;
> print $result, "\n";

The usual term for this operation is 'reduce'; you can find it in
List::Util. Note that you need to explicitly provide an initial value:

use List::Util qw/reduce/;

my $result = reduce { $a || $b } 0, @array;

In this case you could also use 'first':

use List::Util qw/first/;

my $result = first { $_ } 0, @array;

Ben

From: Peng Yu on
On Jul 15, 9:12 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> Quoth Peng Yu <pengyu...(a)gmail.com>:
>
> > Hi,
>
> > I use the following command to OR the values in an array. I'm
> > wondering what is the best way to do so. A function similar to
> > 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
> > in perl.
>
> > my @array=(0,0,1);
> > my $result=0;
> > map { $result = $result || $_} @array;
> > print $result, "\n";
>
> The usual term for this operation is 'reduce'; you can find it in
> List::Util. Note that you need to explicitly provide an initial value:
>
>     use List::Util qw/reduce/;
>
>     my $result = reduce { $a || $b } 0, @array;
>
> In this case you could also use 'first':
>
>     use List::Util qw/first/;
>
>     my $result = first { $_ } 0, @array;

#!/usr/bin/env perl

use strict;
use warnings;
use List::Util;

my @array=1..10;
my $result=List::Util::reduce { $a + $b } @array;
print $result, "\n";
##########

I got the following warnings for the above problem. But the code from
the manual works. I'm wondering what is wrong in the above code?

Name "main::b" used only once: possible typo at ./main.pl line 8.
Name "main::a" used only once: possible typo at ./main.pl line 8.


my $result = List::Util::reduce { $a > $b ? $a : $b } 1..10;#this
works

From: David Bouman on
On Jul 16, 3:27 am, Peng Yu <pengyu...(a)gmail.com> wrote:

> I use the following command to OR the values in an array. I'm
> wondering what is the best way to do so. A function similar to
> 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
> in perl.

What about using a grep in scalar context?

--
David.