Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/perl
- use strict;
- use warnings;
- my @a = qw(a f g h s t);
- my @b = qw(a f g h s t);
- my @c = qw(a f g h s x x);
- my @d = qw(a f g h s t);
- sub compareArr($$;$)
- {
- my ($ar1, $ar2, $max) = @_;
- return 1 if $#{$ar1} == -1;
- if (defined($max) && (($max > $#{$ar1}) || ($max > $#{$ar2})))
- {
- warn "index '$max' is too big...\n";
- return 0;
- }
- $max ||= $#{$ar1};
- my $ret = 0;
- if ($#{$ar1} == $#{$ar2} || defined($max))
- {
- $ret = 1;
- for (my $i = 0; $i <= $#{$ar1} && $i <= $max; $i++)
- {
- return 0 if ($ar1->[$i] ne $ar2->[$i]);
- }
- }
- return $ret;
- }
- print "\@a, \@b: ";
- print compareArr(\@a, \@b) ? 1 : 0;
- print "\n";
- print "\@a, \@c: ";
- print compareArr(\@a, \@c) ? 1 : 0;
- print "\n";
- print "\@c, \@d 6: ";
- print compareArr(\@c, \@d, 6) ? 1 : 0;
- print "\n";
- print "\@c, \@d 5: ";
- print compareArr(\@c, \@d, 5) ? 1 : 0;
- print "\n";
- print "\@c, \@d 4: ";
- print compareArr(\@c, \@d, 4) ? 1 : 0;
- print "\n";
- print "\@c, \@d: ";
- print compareArr([@c[0 .. 3]], [@d[0 .. 3]]) ? 1 : 0;
- print "\n";
Advertisement
Add Comment
Please, Sign In to add comment