in reply to Re: Sorting a matrix based on the values of columns
in thread Sorting a matrix based on the values of columns

G'day ThBo,

Here's a tip for you; prompted by this code:

my $temp = $_[1]; $_[1] = $_[0]; $_[0] = $temp;

You can swap two (or more) array elements using array slices. Your three lines above could've been written, without the need for a temporary variable, as:

@_[0, 1] = @_[1, 0]

Here's an example:

$ perl -wE '@_ = 0 .. 3; say "@_"; @_[0, 1] = @_[1, 0]; say "@_"' 0 1 2 3 1 0 2 3

— Ken

Replies are listed 'Best First'.
Re^3: Sorting a matrix based on the values of columns
by redbull2012 (Sexton) on Mar 11, 2016 at 13:50 UTC

    Thanks a bunch for your enlightening reply :)))

    Any more optimization or beautification suggestion will really be appreciated!

    Nice Weekend!

    ThBo

      Thank you all for the replies. It helped me alot:-)