in reply to All array elements the same?

if (join '', @array1) eq (join '', @array2) { do_stuff; }

Not sure if this will work properly in all cases, but it should work for arrays containing strings. Really though, I can't think of any reason why it would work for numbers and such...

waiting to be proven wrong,

Replies are listed 'Best First'.
RE: Re: All array elements the same?
by davorg (Chancellor) on Aug 01, 2000 at 20:44 UTC

    If you wanted to compare two arrays (and I don't think that's what the original poster wanted) then you could use the Array::Compare module from CPAN which works in a similar manner to your code.


    European Perl Conference - Sept 22/24 2000, ICA, London
RE: Re: All array elements the same?
by Fastolfe (Vicar) on Aug 01, 2000 at 22:34 UTC
    This wouldn't work very well for arrays that concatenate the same but have different values. Consider:
    @one = (undef, 1, 2); # "12" @two = (1, undef, 2); # "12" @one = (12, 3, 4); # "1234" @two = (1, 2, 34); # "1234"
    The Array::Compare module basically does a join() using an unlikely delimiter (like a pipe character or \0 or \255). Unfortunately, my own benchmarks typically put a "brute force" comparison faster than Array::Compare (sometimes significantly, depending on the amount and type of data in the two arrays), especially when dealing with numeric items in the array. Conversions of data types to strings plus string comparisons tend to be expensive in bulk. If you're working with string data, it might not be so bad.