CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Array logic?



Jul 1, 2011, 12:29 PM

Post #1 of 2 (1752 views)
Array logic? Can't Post


I have a situation where I am filling two arrays of length 4 with numbers, only one of which is a zero. So I'll get a result like this:



Before I continue in my program, I need to make sure that I don't have a zero in the same position in each array. For example, I don't want this to happen:



I thought this might be a place for doing logic, "or"ing the two arrays together somehow and checking if all the resulting entries are nonzero.

But I'm stuck.

Any ideas?



Jul 1, 2011, 2:08 PM

Post #2 of 2 (1742 views)
Re: [dwarnold45] Array logic? [In reply to] Can't Post

use the functions "pairwise" and then "any" in the modulle List::MoreUtils.

use strict; 
use warnings;
use List::MoreUtils qw (any pairwise);
our $a;
our $b;
our $c;
my @a=(3, 0,-5, 6);
my @b=(0,-2, 4, 3);
my @c=(4, 0, 4, 3);

die "pair a,b is not compatible\n" if any {$_} pairwise { !$a and !$b } @a, @b;
print 'pair a,b is compatible', "\n";
die "pair a,c is not compatible\n" if any {$_} pairwise { !$a and !$c } @a, @c;
print 'pair a,c is compatible', "\n";

Good Luck,

(This post was edited by BillKSmith on Jul 1, 2011, 3:22 PM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives