# 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;