[Israel.pm] One-liner perl question

Avishalom Shalit avishalom at gmail.com
Sun Sep 9 15:06:36 PDT 2012


$ perl -e '$n=$ARGV[0];@a=map{$_->[1]}sort{$a->[0] <=>
$b->[0]}map{[length($_),$_]} @ARGV[1..$#ARGV]; print "@a[0..$n]"' 4 *

clarifying

$ perl -e '
$n=$ARGV[0];
@a=map{$_->[1]}
        sort{$a->[0] <=> $b->[0]}
        map{[length($_),$_]}
    @ARGV[1..$#ARGV];
print "@a[0..$n]"' 4 *


-- vish




On 9 September 2012 18:39, ynon perek <ynonperek at gmail.com> wrote:
>
> Hi All,
>
> I'm trying to turn a short program into a one-liner. The program takes an argument "n" and prints the first n file names sorted by file name length, with no duplicates in length.
>
> Here's what I wrote for the original code:
> https://gist.github.com/3684778
>
> The part I found difficult is to sort a dictionary by its keys, and print the top n values...
>
> Ideas ?
>
> (clarification: this is just for fun, nothing important, and I'm not interested in string based solutions, already got one on the same gist).
>
> Cheers,
>   Ynon
>
> --
>
> כותב הרצאות ? מדבר מול קהל ? הבלוג שלי לומד לדבר כתוב במיוחד בשבילך.
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl


More information about the Perl mailing list