in reply to How to compare arrays? (xmms alarm clock)

How about:
if ("@current" eq "@wake") { # do something }
Yeah, that's cheating. Yeah, it's ugly. Yeah, it works.

Update: Per tadman's suggestion, one might also do something like and @current == @wake as an additional comparison.

Replies are listed 'Best First'.
Re^2: How to compare arrays? (xmms alarm clock)
by tadman (Prior) on Jun 29, 2001 at 10:23 UTC
    This works so long as both arrays do not contain the $" character (usually space). Otherwise, two lists might "match" even when they don't:
    @a = ('the flying','dorito','brothers'); @b = ('the','flying','dorito brothers'); "@a" = "the flying dorito brothers" "@b" = "the flying dorito brothers"
    So if you're sure that the two arrays are $"-free in that sense, then go ahead.
Re: How to compare arrays? (xmms alarm clock)
by Abigail (Deacon) on Jun 29, 2001 at 17:24 UTC
    This only works because the arrays don't contain a string with $" as substring.

    However, if you have

    @current = ("foo bar", "baz"); @wake = ("foo", "bar baz");
    then both "@current" eq "@wake" and @current == @wake.

    Also, it's not clear what to do when the arrays contain references. Should the references be the same, or is it enough if they point to comparable data? That is, are the following arrays equal?

    @array1 = (1, [2]); @array2 = (1, [2]);

    -- Abigail

Re: Re: How to compare arrays? (xmms alarm clock)
by ginseng (Pilgrim) on Jun 29, 2001 at 08:05 UTC

    my mistake. i tried to implement your idea, and did it wrong. i thought it should have worked and didn't try hard enough. upon further effort it worked exactly as advertised. it even looks clean and understandable. i just wish it were more intuitive to begin with...

    revised...

    do { my @timenow = localtime(time); @current = ($timenow[2], $timenow[1]); sleep 5; } until ("@wake" eq "@current");

    so can i be greedy and ask for more ways to compare the two?

      See How to test equality of hashes? for more ways.

      You can leave out the @timenow:

      @current = (localtime time)[1..2];
      ...just gives you the slice.

      Hope this helps,

      Jeroen

      Update: You're welcome. That prob is easily solved with reverse:

      @current = reverse (localtime time)[1..2];

        This leaves @current with a different array order than @wake has. split /:/ leaves $wake[0] with the hour, and $wake[1] with the minute. localtime[1..2] would return the minute then the hour. to do the comparison, i'd have to invert one array.

        thank you for the hashes link. i'm glad i didn't find it before posting this question. it makes this question look way too easy. as you can see, i've got one solution that works, "@array1" eq "@array2", but it still seems like there'd be a simpler (rather, more intuitive) way. But perhaps my intuition is just not on yet, with Perl.

      How about something like...
      until ((grep {$wake[$_] == $current[$_]} (0..$#wake)) == @wake));

      (NB can't check this right now though)

      andy.

Re: Re: How to compare arrays? (xmms alarm clock)
by ginseng (Pilgrim) on Jun 29, 2001 at 07:40 UTC

    for some reason, that is always true. i haven't thought through why yet. perhaps the array name is being interpreted as a scalar inside of a string?

    i also considered using sprintf on each side of the 'eq', which should definitely work. of course, thats cheating and ugly and inelegant too.

    i suppose i could pack both arrays, and compare the results. still not pretty. there must be something better...