tech2040 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
I am developing programs for image processing. I want to create prototypes in perl however my knowledge of perl scripting is limited. how would I accomplish the following:

You've just loaded a CD's worth of reference images from an outside company. The images follow a naming scheme such as DSCN-1.JPG, DSCN-2.JPG ... DSCN-10.JPG, although the frame numbers may be noncontiguous (i.e., there may be gaps between numbers).

Although the frames were stored in separate directories on the CD-ROM from which they were loaded, all of the frames have now been placed into this directory:

/shots/spi/home/pix/out/home_test_v1/misc_bg8
The structure of the directory you've loaded them into is this:
/shots/$SHOWNAME/$SHOTNAME/pix/out/$ELEMENTNAME/$RESOLUTION_$COLORSPAC +E
Your task is to rename the frames in accordance with SPI standard naming conventions. These include:

1. No capital letters in the filenames

2. No dashes allowed in the filenames

3. The images should be re-numbered so that they are a contiguous sequence (i.e. no gaps)

4. Frame numbers need to be padded to four-digits. ie, "1" becomes "0001" This is denoted by the symbol # so a range of 1-240 would read 1-240#.

5. The frames should be renamed to match the directory in which they are placed in the following manner:

$ELEMENTNAME_$RESOLUTION_$COLORSPACE.#.$EXTENSION
So, in other words, each frame should become:
/shots/spi/home/pix/out/home_test_v1/misc_bg8/home_test_v1_misc_bg8.00 +01.jpg /shots/spi/home/pix/out/home_test_v1/misc_bg8/home_test_v1_misc_bg8.00 +02.jpg /shots/spi/home/pix/out/home_test_v1/misc_bg8/home_test_v1_misc_bg8.00 +03.jpg ... /shots/spi/home/pix/out/home_test_v1/misc_bg8/home_test_v1_misc_bg8.02 +40.jpg
However, the incoming frames are part of a series and must be kept in the exact same order as they were when loaded (i.e., the incoming frame with the lowest index will map to `home_test_v1_misc_bg8.0001.jpg', while the second lowest index will map to `home_test_v1_misc_bg8.0002.jpg', and so on).

Please help

Replies are listed 'Best First'.
Re: file sorting question
by sk (Curate) on Aug 14, 2005 at 02:52 UTC
Re: file sorting question
by Zaxo (Archbishop) on Aug 14, 2005 at 02:46 UTC
Re: file sorting question
by TedPride (Priest) on Aug 14, 2005 at 21:51 UTC
    The only really complicated part here is how to efficiently sort the file names by their numerical component. The following shows how this can be done:
    use strict; use warnings; my %names; # Extract numerical component of each file name and create # key (name) / value (number) pair while (<DATA>) { chomp; ($names{$_}) = m/(\d+)/; } # Sort the keys by their values (numerically) and print print "$_\n" for sort {$names{$a} <=> $names{$b}} keys %names; __DATA__ DSCN-1.JPG DSCN-10.JPG DSCN-12.JPG DSCN-3.JPG DSCN-7.JPG