It does the job tho' :)

Ok. With comments...

# define a hash to count the unique elements in @arr my %check; # @check{@arr} is a hash slice. It gives to all of the # values in %check where the key is an element from @arr. # This is a list and it's also an lvalue (i.e. you can # assign values to it. # # (1) x @arr (and I've corrected this from the original # version. Uses the 'x' operator as a list constructor. # In a list context it creates a list containing its # left operand repeated the number of times given by # the right operand (which is a scalar). @arr in a scalar # context gives the number of elements in @arr. Hence # we get a list containing scalar(@arr) 1s. # # We then assign this list to the list of lvalues created # by @check{@arr}. This assigns a 1 to each value associated # with a key found %check. The keys of %check are given by # @arr, therefore we effectively create a key/value pair # in %check for each element of @arr where the key is the # element of @arr and the value is 1. If any element of # @arr occurs multiple times we assign to the relevant # value in %check multiple times. # # We can then use 'keys' to extract this list of keys. If # all the elements of @arr are the same the list of keys # in %create only has one element. @check{@arr} = (1) x @arr; print "All the same" if keys %check == 1;

Does that help?</code> --
<http://www.dave.org.uk>

European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/>


In reply to RE: RE: Re: All array elements the same? by davorg
in thread All array elements the same? by Grumpy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.