in reply to comparing arrays

I know everybody is all concerned about speed, and how efficent things are in this thread, so I'm not 100% sure how good this snippet is, but hey, tmtowtdi.

#!/usr/bin/perl -w use strict; my @r = ('j','r','t','string value','numeric'); my @x = ('x','r','t','string value','numeric'); print join('',@r)," :array 1\n"; print join('',@x)," :array 2\n"; print "\n"; print "arrays match\n" if ((join('',@r))eq(join('',@x)));


_14k4 - perlmonks@poorheart.com (www.poorheart.com)

Replies are listed 'Best First'.
Re: Re: comparing arrays
by andye (Curate) on Apr 04, 2002 at 14:35 UTC
    IMHO, better to join them with a character that you know doesn't appear in the array's contents, e.g. \0 or pipe or something. Otherwise - unless I'm misunderstanding your code - it'll think ('ab', 'c') equals ('a', 'bc').

    Cheers,
    andy.

        I thinks, that this is a question not of quick coding, but of losing valueable information about the structure of your data. You can compare some derivatives of the arrays if and only if that's a reversible derivative. Think of stringification of an array which could always be transformed back into the original array. You could e.g. escape all spaces in the array components and then join with spaces. This would yield a string which can be used for comparison.
        No problemo. Actually, rather than using join(), I'd be tempted to do something like this:
        { local $" = "\0"; print "match" if "@a" eq "@b"; }
        (not tested, may have typos).

        andy.