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);

In reply to How to compare arrays? (xmms alarm clock) by ginseng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.