A. Sinan Unur said:
Davy wrote:
Can I use if(@a==@b) directly?
[...]
But it's better practice to do
if ( $#a == $#b ) { ... }
Why?
I'ld say because with $#arrayname it is immediately clearer that we're
dealing with the number of elements,
Boolean conditions provide scalar context. In that context, @a is the
number of elements in @a, whereas $#a is one less than the number of
elements in @a.
One can actually compare arrays for equality in a single statement if
the contents of the arrays are simple strings or numbers:
#!/usr/bin/perl
use strict;
use warnings;
my @a = qw(one two three);
my @b = qw(one two three);
my @c = (1, 2, 3);
my @d = (1.0, 2.0, 3.0);
print 'Comparing @a and @b:', "@a" eq "@b" ? "equal\n" : "not equal\n";
print 'Comparing @c and @d:', "@c" eq "@d" ? "equal\n" : "not equal\n";
print 'Comparing @a and @d:', "@a" eq "@d" ? "equal\n" : "not equal\n";