|Syntactic Confectionery Delight|
How to compare arrays? (xmms alarm clock)by ginseng (Pilgrim)
|on Jun 29, 2001 at 07:15 UTC||Need Help??|
ginseng has asked for the
wisdom of the Perl Monks concerning the following question:
I had reason to want to know if two arrays contained the same values, in the same order. Okay, -vv me...I had an appointment I was worried about missing, but i really had to lie down on the couch in my office and get some zzz's. (I did deserve them, honest.) So i wrote a quick script to run xmms at a given time, and set the mixer volume up all the way.
To accomplish this, I took the command line argument and split it on ':', assuming military time. That goes into the @wake array. The current time hour and minute go into @current. Now I want to know, for my while loop, whether the two arrays are the same.
Doing (@current == @array) treats the arrays as scalars, and is true, since both arrays have the same number of members. Doing (($current, $current) == ($wake, $wake)) also didn't work, though I didn't know it until the minutes were equal and the hours were not...apparently, it only compares the last element. I tried ([@current] == [@wake]), using anonymous arrays (which I scarsely understand), and it didn't work. In short, the only thing that worked was an element by element comparison. Not very elegant (especially if the two arrays have more than two elements!)
So I'm sure this is one of those silly newby questions that anyone with ten minutes more experience than me will be able to answer quickly. What is the right way to compare two arrays?
On a side note, the code worked, I slept through all of the Chess soundtrack at full volume, and woke up hours late :(
Want to see the code for the alarm clock anyway, despite the fact it failed to wake me?