in reply to Re: Why does 'print shift sort @array' not work?
in thread Why does 'print shift sort @array' not work?

What's the value of shift @{[sort @array]} over (sort @array)[0]?

Replies are listed 'Best First'.
Re^3: Why does 'print shift sort @array' not work?
by hbm (Hermit) on Feb 26, 2009 at 20:19 UTC

    I said "another", not "a more valuable." As for preferable, I think it depends. Consider:

    print pop @{[sort qw(a f e b)]}; print (reverse sort qw(a f e b))[0]; use List::Util qw(maxstr); print maxstr qw(a f e b);

      pop @{[ sort qw(a f e b) ]}
      (Sort, make an array and a reference to it, dereference it, get the last item.)

      would be

      ( sort qw(a f e b) )[-1]
      (Sort, get the last item.)

      min/minstr/max/maxstr is both shorter, less work, and more straightfoward, but it is not nearly as flexible.

      maxstr qw(a f e b)
      (Get max item.)

Re^3: Why does 'print shift sort @array' not work?
by Fletch (Bishop) on Feb 26, 2009 at 20:30 UTC

    Other than that print (sort @array)[0] would be a syntax error without a + snuck in the right place? :)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      We both known Perl is very finicky when parens are omitted. But
      print [sort @array]->[0]

      would still be simpler, no?

        But that's a whole character longer . . . :) (But then again the square version requires hitting shift one less time so it has that going for it :)

        print +(sort @array)[0]; print [sort @array]->[0];

        Possibly could an interesting benchmark if anyone's so inclined: does slicing the value off the stack beat constructing the anonymous arrayref (which gets tossed immediately) then deref'ing, and what the falloff is as @array grows.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        Aren't the following functionally equivalent, i.e. Sort, make an array and a reference to it, dereference it, get the [first] item?

        print [sort @array]->[0]; print @{[sort @array]}[0];
    A reply falls below the community's threshold of quality. You may see it by logging in.