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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Array logic?

 



dwarnold45
Novice

Jul 1, 2011, 12:29 PM

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

All,

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:

@a=(3,0,-5,6);

@b=(0,-2,4,3);

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:


@a=(3,0,-5,6);

@b=(4,0,4,3);

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?

David


BillKSmith
Veteran

Jul 1, 2011, 2:08 PM

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

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



Code
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,
Bill

(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