So, I have a big directory full of images. The naming pattern is <Number>.<Counter>.jpg (where counter is 000, 001, 002...). I have a list of numbers which match up with Numbers that I want to keep (so for a given Number in the list, keep Number.000.jpg, Number.001.jpg, etc).

As far as I can tell, the following code should work, but the numbers it reports don't match up. Any idea what's going on?

The current output is:

33600 images in photos (8321 unique). 6571 numbers in numbers_only.txt. 5999 numbers slated for deletion.
#!/usr/bin/perl -w use strict; my $num_file = "numbers_only.txt"; my %currentlist; my $i = 0; open(CURRFILE, "<$num_file") or die "couldn't open the num file: $!\n" +; while (<CURRFILE>) { chomp; $currentlist{$_}++; $i++; } close CURRFILE; my $dir = "images/photos"; opendir FILEDIR, $dir or die "Couldn't open $dir: $!\n"; my @filelist = grep !/^\.\.?$/, readdir FILEDIR; closedir FILEDIR; my %uniquelist; my %deletelist; my $h = 0; foreach my $pic (@filelist) { next unless ($pic =~ /jpg$/); $pic =~ s/^(\d+).*/$1/; $h++; $uniquelist{$pic}++; next if ($currentlist{$pic}); $deletelist{$pic}++; } my $output = "delete_list.txt"; open OUT, ">$output" or die "Couldn't open output: $!\n"; my $j = 0; foreach my $del (sort keys %deletelist) { print OUT "$del\n"; $j++; } close OUT; my $k = 0 + keys %uniquelist; print "$h images in photos ($k unique). $i numbers in numbers_only.txt +. $j numbers slated for deletion.\n"; exit;

In reply to Build a list of files that are NOT in a list by amelinda

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.