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

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,0,undef) and takes only the elements that are true, this case 1. Back to your question:
    @sizes = grep {length $_ -> [1]} @sizes;
    This goes thru @sizes (which is a list of array references) and takes just the elements where the length of the second index of the array ref has length greater than zero. This is quick way to write:
    my @new_sizes;
    foreach my $size (@sizes) {
        if (length $size->[1]) {
            push(@new_sizes, $size);
        }
    }
    @sizes = @new_sizes
    --

    --
    "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.