in reply to Comparing sorted arrays
Note that I used string equality. If the arrays are going to contain only numbers, then you'd want to use numeric comparison (==) instead. As an added syntactic nicety, you can write this subroutine with a prototype, so you don't have to pass the arrays explicitly as references:# pass it two arrays, by reference. sub arrays_equal { my( $ar1, $ar2 ) = @_; @$ar1 eq @$ar2 or return 0; # different lengths. # now do member-wise comparison: for my $i ( 0 .. $#{$ar1} ) { $ar1->[$i] eq $ar2->[$i] or return 0; # short circuit } return 1; # all members compared equal. } # call it like so: if ( ! arrays_equal( \@savedpids, \@livepids ) )
If you use the prototype, however, the declaration of the sub must come before the point of first use.sub arrays_equal(\@\@) { # everything else is the same } # then you can call it like so: if ( ! arrays_equal( @savedpids, @livepids ) )
jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.
|
|---|