suhailck has asked for the wisdom of the Perl Monks concerning the following question:

hi, i have one function to perform quicksort
and im not clear about one line in the function on how it
works
the function is ,

sub quicksorty {
return " @_ " if @_ < 2;
print "My pivot is $_[0]\n";
my ( @l, @e, @g );
push @{ ( \@e, \@g, \@l )[ $_ <=> $_ 0 ] }, $_ for @_; #
here's the line
quicksorty( @l ), " @e ", quicksorty( @g );
}
Can neone plz explain the working of particular line
push @{ ( \@e, \@g, \@l )[ $_ <=> $_ 0 ] }, $_ for @_;

Thanx in advance.

Replies are listed 'Best First'.
Re: help with the function
by ysth (Canon) on Nov 03, 2008 at 06:27 UTC
    The <=> compares $_ and $_[0] and returns -1, 0, or 1, depending on whether $_ is less than, equal to, or greater than $_[0]. The (LIST1)[LIST2] syntax is a list slice - it returns the elements of LIST1 specified in LIST2. LIST2 is the -1, 0, or 1, which respectively get the last, first, or second element of LIST1, so it gets \@e if they were equal, \@g if $_[0] was the greater, and \@l if $_[0] was the lesser. The @{EXPR} syntax treats EXPR as an arrayref and uses the referred to array (so @e, @g, or @l), and the push pushes $_ onto that array. The for loops over @_, aliasing $_ to each element in turn, doing the comparison to the first element and the push to different arrays depending on the result of the comparison.
      ++ysth for an admirably clear explanation, and good luck to suhailck on his or her homework/exam problem.