Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: All array elements the same?

by young perlhopper (Scribe)
on Aug 01, 2000 at 20:28 UTC ( #25491=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://25491]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2023-09-27 10:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found