0

When I have a folder with about 5000 images and I use the following code:

my $path= "./foo/images"
my @files = File::Find::Rule->file()->name('*.jpg')->in($path);

I should get 5000 Images in my Array.

But are there equivalents with perl or a shell function like tail and head, or any other way, to get the last 100 images for example?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Robin93K
  • 198
  • 2
  • 3
  • 15

2 Answers2

1

You can use an array slice. This gets the 1st 5 returned:

my @files = (File::Find::Rule->file()->name('*.jpg')->in($path))[0 .. 4];

This gets the last 5:

my @files = (reverse File::Find::Rule->file()->name('*.txt')->in($path))[0 .. 4];
toolic
  • 57,801
  • 17
  • 75
  • 117
0

Just realised... File::Find::Rule completly mixes up the images... so we need a sort additionally before slicing the Array

so finally I/we have to use the following:

for the first 5:

my @files = (sort File::Find::Rule->file()->name('*.jpg')->in($path))[0 .. 4];

for the last 5:

my @files = (reverse sort File::Find::Rule->file()->name('*.jpg')->in($path))[0 .. 4];
Robin93K
  • 198
  • 2
  • 3
  • 15