Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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,
Mark

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.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-11 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?