If you numerically sort your filenames in descending order, you will have the biggest in the first element (index 0) and the next biggest in the next element.

Modifying the code I provided earlier:

#!/usr/bin/perl -l use strict; use warnings; use autodie; my $dir = '.'; opendir(my $dh, $dir); my @emptyfiles = sort { $b <=> $a } grep { -f && /^\d+$/ && -z } readd +ir $dh; closedir $dh; print for @emptyfiles; print "Biggest: $emptyfiles[0]"; print "Next biggest: $emptyfiles[1]";

Gives this output:

123 1 Biggest: 123 Next biggest: 1

So, they're effectively in separate variables already (i.e. $emptyfiles[0] and $emptyfiles[1]). There's probably no need to use other variables but, if you really need to, just do something like this (untested):

my ($biggest, $next_biggest) = @emptyfiles[0, 1];

Regarding your problem with "It keeps saying that $previous is -1", there's at least two problems. Firstly, the code you posted

$previous = ($emptyfile)-1);

won't compile. You'll get something like: "syntax error at ... line ..., near "1)""

So, the code you posted isn't the code you're running; however, given you don't initialise $emptyfile, subtracting 1 from it will evaluate to -1 and should also give you a warning (assuming you still have use warnings; at the top of your code). Here's a minimal example:

$ perl -wE 'my $x; say($x - 1)' Use of uninitialized value $x in subtraction (-) at -e line 1. -1

— Ken


In reply to Re^2: create array of empty files and then match filenames by kcott
in thread create array of empty files and then match filenames by angela2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.