in reply to Type of arg 1 to shift must be array (not sort)

perl-ish:
#!/usr/bin/perl -w use strict; my @array = qw( b d c a e g f ); print shift @{[ sort @array ]};

Replies are listed 'Best First'.
Re^2: Type of arg 1 to shift must be array (not sort)
by wazzuteke (Hermit) on Dec 15, 2005 at 22:38 UTC
    Note that this is true with a number of other different perlfuncs that return as immutable lists rather than arrays... copying the de-referenced return and forcing it back into an array will cure most non-perlish headaches.

    For example:
    #!/usr/bin/perl use strict; use warnings; my @array = qw( a b c d e f ); # With sort.. my $sorted = shift( @{ [ sort( @array ) ] } ); # With grep my $grepped = shift( @{ [ grep{ /\w+/ } @array ] } ); # With map my $mapped = shift( @{ [ map{ $_ } @array ] } ); # As a stringified method call my $method = "@{ [ FooBar->new()->a_method() ] }"; print "$sorted\n$grepped\n$mapped\n$method\n"; package FooBar; use strict; use warnings; sub new { return bless {}, shift; } sub a_method{ return 'some string'; }
    Yay for Perl!

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );
      I don't know why you'd waste the time and space to array-ify the return value when a literal slice will work just as easily for those. Instead of:
      my $result = shift @{[some list function]};
      just write
      my $result = (some list function)[0];
      A whole lot easier on the CPU and the eyes. That's why I invented the literal slice (one of the few things I can lay direct claim to in Perl).

      Of course, if you're going to store it into a scalar like this, you can simplify it further:

      my ($result) = some list function;

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re^2: Type of arg 1 to shift must be array (not sort)
by davidrw (Prior) on Dec 15, 2005 at 21:30 UTC
    similar to my first thought which was:
    perl -le '@x=(9,8,4,3,6,5); print ((sort @x)[0])'
    BUT not what OP wanted .. he wants @array to contain N-1 values at the end.. Something like (though not the most efficent):
    perl -le '@x=(9,8,4,3,6,5); print join ":", ((sort @x)[1..$#x])' # OP: @array = sort @array; shift @array; #using above: @array = ((sort @array)[1..$#array])