### Re^2: So what is an array slice anyway?

by beable (Friar)
 on Aug 26, 2004 at 04:52 UTC ( #385907=note: print w/replies, xml ) Need Help??

in reply to Re: So what is an array slice anyway?
in thread So what is an array slice anyway?

That's an interesting thing to be doing. However, I think the person who asked me the question is actually trying to find out whether arrays and array slices are "equal" by using the "==" operator. I tried to explain how it was comparing the number of elements in arrays by using scalar context, but he suddenly went to bed. I was hoping there would be a function in List::Util to compare arrays and return if they are equal, but no luck there. Sorry for not making that clear before. What he wants to do is to 1) compare arrays to see if they have all the same elements in, and 2) compare array slices to see if they have all the same elements in. I think he should just write a subroutine to do it. But I sure think he shouldn't be using "==" to do it.
• Comment on Re^2: So what is an array slice anyway?

Replies are listed 'Best First'.
Re^3: So what is an array slice anyway?
by Aristotle (Chancellor) on Aug 26, 2004 at 08:13 UTC

Here's an idiomatic way to test whether the contents of two arrays are equal — assuming the elements are numbers, anyway.

```print "equal\n"
if @array1 == @array2
and !grep \$array1[ \$_ ] != \$array2[ \$_ ], 0 .. \$#array1;

In scalar context, grep evaluates to the number of elements it returned, so that condition says "if both arrays have the same length, and if no elements are found which are not equal".

Makeshifts last the longest.

Re^3: So what is an array slice anyway?
by beable (Friar) on Aug 26, 2004 at 07:20 UTC
Okay, here's what I'm going to give him. Hopefully it will make him think again about using == for this sort of thing. Hopefully it has enough examples and pointers to the documentation to show him what's up.

Re^3: So what is an array slice anyway?
by McMahon (Chaplain) on Aug 26, 2004 at 14:29 UTC
I do this sort of thing a lot. List::Compare makes it pretty easy.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://385907]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2023-03-21 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (60 votes). Check out past polls.

Notices?