in reply to Re: create array of empty files and then match filenames
in thread create array of empty files and then match filenames
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: create array of empty files and then match filenames
by angela2 (Sexton) on Jan 14, 2016 at 14:28 UTC | |
by kcott (Archbishop) on Jan 15, 2016 at 00:12 UTC | |
by poj (Abbot) on Jan 14, 2016 at 15:52 UTC | |
by angela2 (Sexton) on Jan 14, 2016 at 16:16 UTC |