hoffmann has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have four column arrays "one," "two," "three," and "four." Each array contains 500 scalar values (500 rows x 1 column). How can I say the following: If any one of these scalar values (in any array) are equal to zero then execute the following code, whatever it may be. Do I need to create a new array containing the indices of the zero elements in "one" "two" "three" and "four"? How can I code this?

Replies are listed 'Best First'.
Re: "Or" in Array
by toolic (Bishop) on Jul 24, 2008 at 00:37 UTC
    If you want to perform this check once per row, you could loop though the arrays:
    use strict; use warnings; my @one = qw(5 6 7); my @two = qw(1 2 0); my @three = qw(1 1 1); my @four = qw(9 4 6); for my $i (0 .. $#one) { unless ($one[$i] and $two[$i] and $three[$i] and $four[$i]) { print "row $i has a zero\n"; # execute some code } }

    prints:

    row 2 has a zero

    Update: You should consider reorganizing your data as an ARRAYS OF ARRAYS structure, if you have control over your data. In general, it is easier passing around one data structure than 4.

Re: "Or" in Array
by NetWallah (Canon) on Jul 24, 2008 at 00:38 UTC
    Presumably, you wanted to operate on the locations which contained "0" values.

    Here is code that shows how to do so:

    #!/usr/bin/perl -w use strict; my (@one,@two,@three,@four); for (\@one,\@two,\@three,\@four){ # Populate them @$_ = map {int (rand() * 10)} 0..499; } my $zerocount = 0; my $index = 0; my $array = 0; for (\@one,\@two,\@three,\@four){ # Look for zero, and change it to 999. $index = -1; for (@$_){ $index ++; next unless $_ == 0; print " Found a zero in array $array at $index. inserting 999\ +n"; $_ = 999; # Changes the value!!!! } $array ++; }

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: "Or" in Array
by injunjoel (Priest) on Jul 24, 2008 at 00:20 UTC
    Greetings, You can use grep to see if the value is in there. For example...
    #!/usr/bin/perl -w use strict; #make up some data my @arr1 = 1 .. 100; my @arr2 = 0 .. 300; my @arr3 = reverse(@arr1); my @arr4 = 5 .. 10; #test it. if(grep($_ == 0, (@arr1,@arr2,@arr3,@arr4))){ print "Found it!"; }else{ print "no dice!"; }
    this prints
    Found it!


    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: "Or" in Array
by poolpi (Hermit) on Jul 24, 2008 at 12:45 UTC

    See List::MoreUtils and any

    #!/usr/bin/perl -w use strict; use List::MoreUtils qw(any); use List::Util qw(shuffle); my ( @one, @two, @three, @four ); my @arrays = ( \@one, \@two, \@three, \@four ); map { @$_ = shuffle map { int rand 10 } 1 .. 500; } @arrays; # If >any< one of these scalar values (in any array) are equal # to z +ero then execute the following code map { print "Zero found!\n" if any { $_ == 0 } @$_ } @arrays;

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb