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[0], $current[1]) == ($wake[0], $wake[1])) 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?
#!/usr/bin/perl use strict; use POSIX; unless (!@ARGV || ($ARGV[0] =~ /:/)) { print "Usage: $0 [hour:min]\nThis program will start XMMS with the c +urrent playlist at high volume at a given time. If hour: min is not given, the alarm will go off almost immediately.\n"; exit; } my @wake = split /:/, $ARGV[0]; my @current; if (@ARGV) { print "Alarm is set for $wake[0]:$wake[1].\n"; do { my @timenow = localtime(time); @current = ($timenow[2], $timenow[1]); sleep 5; } while (($wake[0] != $current[0]) || ($wake[1] != $current[1])); } if (my $pid = fork) { # parent print "It's $wake[0]:$wake[1] now.\n"; my $mixpid; unless ($mixpid = fork) { exec "mixer vol 100:100"; } waitpid($mixpid, 0); waitpid($pid, 0); } else { # child exec "xmms -p"; } my $mixpid; unless ($mixpid = fork) { exec "mixer vol 75:75"; } waitpid($mixpid, 0);
|
---|