Can I compare array with array?

D

Davy

Hi,

If I have two array, like @a = (1,2,3); @b = (1,2,4);
Can I use if(@a==@b) directly?
Thanks!

Davy
 
B

Bart Van der Donck

Davy said:
If I have two array, like @a = (1,2,3); @b = (1,2,4);
Can I use if(@a==@b) directly?

You can use that check only to find out whether the number of elements
in both arrays are the same, and _not_ whether their values match.

But it's better practice to do

if ( $#a == $#b ) { ... }

(and better practice to not use $a and $b as variable names, because
they could have a special meaning)

Hope this helps,
 
J

Jürgen Exner

Bart said:
Davy said:
If I have two array, like @a = (1,2,3); @b = (1,2,4);
Can I use if(@a==@b) directly?
[...]
But it's better practice to do

if ( $#a == $#b ) { ... }

I disagree. Why should that be better?
Ususally the $# notation should be used when you are explicitely looking for
the index while scalar() should be used when you are looking for the size.
(and better practice to not use $a and $b as variable names, because
they could have a special meaning)

But he's using @a and @b, which have nothing to do with $a and $b

jue
 
B

Bart Van der Donck

A. Sinan Unur said:
Davy said:
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, while @arrayname is more perceived
as the array as a whole.
But he is not using those names.

Apparently I mistook the @ for a $ when I typed that.
 
J

Jürgen Exner

Bart said:
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,

But we are not. $#arrayname is the last index in that array, not the number
of elements.
while @arrayname is more
perceived as the array as a whole.

That depends on the context ;-)

jue
 
B

Bart Van der Donck

A. Sinan Unur said:
[...]
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

It seems I was not well aware of the difference between $#array and
@array when it comes to the number of elements in an array.
 
D

Dr.Ruud

A. Sinan Unur schreef:
$#a is one less than the number of
elements in @a.

Generally yes, as it is the last index in the array.

my @a = qw(one two three);
my @b = qw(one two three);

perl -wle '
$, = qq/\t/;
@a = qw(a b c);
@b = (qq/a$"b$"c/);
print qq/@a/ eq qq/@b/, 0+@a, 0+@b
'
 
P

Paul Lalli

Dr.Ruud said:
A. Sinan Unur schreef:


Generally yes, as it is the last index in the array.

"Generally"? Can you please explain? In what situation do you believe
the condition:
@a - 1 == $#a
to return a false value?
perl -wle '
$, = qq/\t/;
@a = qw(a b c);
@b = (qq/a$"b$"c/);
print qq/@a/ eq qq/@b/, 0+@a, 0+@b
'

What, exactly, are you attempting to prove here? That "@a" eq "@b" is
not a good enough check to prove that @a and @b actually contain the
same values? While I completely agree with you, I'm trying to figure
out what that has to do with anything else you said.

Please explain.

Paul Lalli
 
J

Jürgen Exner

Paul said:
"Generally"? Can you please explain? In what situation do you
believe the condition:
@a - 1 == $#a
to return a false value?

Every time someone messed around with $[
Of course use of $[ is highly discouraged, but that doesn't mean that some
people aren't stupid enough to try it nevertheless.

jue
 
D

Dr.Ruud

Jürgen Exner schreef:
Of course use of $[ is highly discouraged, but that doesn't mean that
some people aren't stupid enough to try it nevertheless.

I happen to like arrays that don't have to start at 0. In Visual Basic I
used them a lot.
But it should be defined per array, not per file, so $[ary and $#ary.
AFAIK, Perl6 has it.
 
D

Dr.Ruud

Paul Lalli schreef:
In what situation do you
believe the condition:
@a - 1 == $#a
to return a false value?

See jue's reply.

What, exactly, are you attempting to prove here? That "@a" eq "@b" is
not a good enough check to prove that @a and @b actually contain the
same values? While I completely agree with you, I'm trying to figure
out what that has to do with anything else you said.


I cut too much, this was above the "my @a = qw(one two three)"
 
C

Charles DeRykus

A. Sinan Unur said:
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";

Some oddballs will break that, eg,

my @c = ( " ", " ", );
my @d = ( " ", " " );


On the other hand, if you knew the array data contained printable char's
only, there's a better solution:

join("\0", @c) eq join("\0", @d) ? 'equal' : 'not equal'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top