Skip to content

Instantly share code, notes, and snippets.

@smujohnson
Created September 7, 2013 21:48
Show Gist options
  • Save smujohnson/6479630 to your computer and use it in GitHub Desktop.
Save smujohnson/6479630 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use 5.010;
use warnings;
use strict;
# Important: This script will ONLY print the image links if both the thumbnail
# and original image are found.
# ---
my $string_base = '<a href="%u.jpg" class="fancybox" rel="gallery"><img src="%u-thumb.jpg" /></a>';
my $scandir = '/tmp/sjohnson/images/';
# ---
opendir(DIR, $scandir) || die "can't opendir $scandir: $!";
# scan all files not matching ., .., or non 2-digit numerics
my @files = grep { $_ ne '.' && $_ ne '..' && m/^\d{2}/ } readdir(DIR);
closedir(DIR);
# a hash to just keep track of which 2 digit number codes we've seen
my %images_bank;
foreach (@files) {
my $number_prefix = substr($_, 0, 2);
if (! exists($images_bank{$number_prefix})) {
if (-f "$scandir/$number_prefix.jpg" && -f "$scandir/$number_prefix-thumb.jpg") {
# the 1 only means that "I've seen this number", it is not used for
# anything other than just saying that I simply "saw it" already in the
# hash table.
$images_bank{$number_prefix} = 1;
}
}
}
my @image_numbers_by_seen = sort {$a <=> $b} keys %images_bank;
foreach my $number (@image_numbers_by_seen) {
printf("$string_base\n", $number, $number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment