use strict; my @a = qw(a a a a a a a); my @b = qw(a a a a a a Q); my @c = qw(a a a M a a a); my @d = qw(Z a a c a a a); sub all_eq { my $first = shift; for (@_) { next if ($first eq $_); return 0; } return 1; } map { printf "testing @$_: -> %d\n", all_eq(@$_) } (\@a, \@b, \@c, \@ +d);

Replies are listed 'Best First'.
Re (tilly) 1: Golf Challenge: all elements of an array equal
by tilly (Archbishop) on Jul 14, 2001 at 01:26 UTC
    17 chars.
    sub all_eq { !grep$_[0]ne$_,@_ }
Re: (Golf) All Elements of an Array Equal
by tadman (Prior) on Jul 14, 2001 at 07:36 UTC
    Like MeowChow's, 17 characters, but this one can be run more than once without error:
    sub all_ne { map{$_ ne$_[0]}@_ }
    Update:
    Upon reading the entire thread, this does seem similar to what tilly posted. Strange. This means that I can now say:
    sub all_ne { map$_[0]ne$_,@_ }
    Which is incredibly only 15 characters. Thanks to tilly for the obvious parameter re-ordering trick. I had been mashing around with grep during testing, but discarded it as too bulky.

    Update-2:
    For whatever reason, it seems I've implemented the function in the negative sense.
      You should have stayed with grep. Try the following:
      sub f{ map$_[0]ne$_,@_ } print "Gotcha!\n" if f(1,2);
Re: Golf Challenge: all elements of an array equal
by jmcnamara (Monsignor) on Jul 14, 2001 at 02:45 UTC

    sub all_eq0 { @_{@_}=2; !((@_=%_)-2) } # 21 chars but wrong as per MeowChow sub all_eq1 { local%_; @_{@_}=2; !((@_=%_)-2) } # 29 chars sub all_eq2 { my%h; @h{@_}=2; !((@_=%h)-2) } # 26 chars
    Update: Spotted the mistake in all_eq0() but MeowChow beat me to it. And then beat me some more. ;-)

    John.
    --

      Interesting method. The bad news is that it can only be used once (try princepawn's test case, setting @d = qw/a a a a a a a/). But the good news is that it can be reduced to 18 :)
      sub all_eq { (@_{@_}=1)==keys%_ }
      update: 17 chars...
      @_{@_}=0;keys%_<2
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
Re: Golf Challenge: all elements of an array equal
by jmcnamara (Monsignor) on Jul 14, 2001 at 12:21 UTC

    A variation on the theme of map and grep in an avoid context:
    sub all_eq3 { @_=sort@_; $_[0]eq pop } # 21 chars


    John.
    --

      My first instinct was to go the sort route, actually. You could shrink your solution by one:

      sub all_eq { #123456789_123456789_ (@_=sort@_)[0]eq pop }

      His Royal Cheeziness

Re: Golf Challenge: all elements of an array equal
by jmcnamara (Monsignor) on Jul 14, 2001 at 23:47 UTC

    I thought of this in the car this morning:
    sub all_eq5 { "@_ "eq"$_[0] "x@_ } # 18 chars
    Update: Unfortunately, this methodology is flawed. It returns the wrong value for a list such as ('aa', 'aaa', 'a').

    John.
    --

Re: Golf Challenge: all elements of an array equal
by jmcnamara (Monsignor) on Jul 14, 2001 at 12:23 UTC

    And another ...
    sub all_eq4 { local$"=''; "@_"eq$_[0]x@_ } # 25 chars
    Update: Unfortunately, this methodology is flawed. It returns the wrong value for a list such as ('a a', 'a a a', 'a').

    John.
    --

Re: Golf Challenge: all elements of an array equal
by nakor (Novice) on Aug 14, 2001 at 18:47 UTC
    in perl6 (if Damian's goal of including Quantum::Superpositions comes true): all@a==all@b 12 chars! Woohoo!