Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

xenchu (4746)

xenchu
  (email not shown publicly)
http://www.uncc. ... ert/vampfilm.htm
Yahoo! ID: xenchu (Add User, Send Message)

The vapid tale of a boring layabout. I mean I don't even drink. I like women, books and Perl (hmmm, yeah, that's about the right order).

Journal of xenchu (4746)

Tuesday February 03, 2004
11:03 PM

Perl Grep Question

[ #17195 ]

This is a line I pulled out of a program on PerlMonks to find the largest 10 files in a directory. The program is clear enough, I suppose, but I don't understand the following line:

     @sizes = grep {length $_ -> [1]} @sizes;

Specifically, what is

{length $_ -> [1]}

doing? It seems to be sorting files by length but I don't understand the mechanism. Anyone care to elucidate?

I saw Windtalkers on DVD tonight. Lots of action, lots of explosions; I think Joe Bob Briggs would give it a thumbs up. I liked it and believe it is worth your time.

Thanks to merlyn and ybiC for their replies to my last entry. As a matter of fact I have a copy of Learning Perl. Unfortunately it is packed in one of dozens of boxes as part of our coming move. I am loathe to buy another copy of a book I already have. However, since I don't yet have a copy of Elements of Programming with Perl I plan to buy that. Scrooge McDuck ain't in it with me when it comes to pinching pennies.

I am going to learn Perl. As long it takes, whatever it takes, but learn it I will. A short attention span, dog-laziness, carelessness past comprehension and a thick head will not stop me.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • @sizes = grep {length $_ -> [1]} @sizes; Specifically, what is {length $_ -> [1]} doing?

    From the docs:

    Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consist- ing of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.

    The most basic example is something like:

    perl -e '@foo = grep { $_ } (1,0,undef); print "@foo\n"'

    This goes though the list (1,

    --

    --
    "Cake or Death!" .... "Cake please"
    • Oh. Ow. Ouch! @size is not an array of file names but rather a line with file name, file size, etc. I might plead that I am a windows programmer but geez, even so, I should have realized that. My stumbling block was [1]. I couldn't see the relationship even knowing what -> was. Density^3. Thank you.

  • I'm curious about the rest of the code. What happened to @sizes before that? Where can I see that?
    • Certainly. This was written by Abigail-II in answer the question Finding Top 10 Largest Files on PerlMonks. Since Abigail-II wrote it I have no doubt as to the correctness of the program:

      #!/usr/bin/perl

      use strict;
      use warnings;
      no warnings qw /syntax/;

      open my $fh => "find / -type f |" or die;

      my @sizes = map {[-1 => ""]} 1 .. 10;

      while (<$fh>) {
          chomp;
          my $size = -s;
          next if $size < $sizes [-1] [0];
          foreach my $i (0 .. $#sizes) {