wiz has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; # specify the full path to the image files in $base_dir # do not use \ on win32 use / Perl will convert like magic! # \ is the escape char. Just *don't use it* for paths # use / and let Perl take care of the conversion. my $base_dir = 'e:/rgw2/cofsrjpg'; # first let's just read all the files in the dir # and print them out, this is how we do it: opendir (DIR, $base_dir) or die "Unable to open dir: $base_dir Perl sa +ys: $!\n"; while (my $file = readdir DIR) { print "found file $file\n"; } close DIR; # OK so now we can read all the files in a dir # we can look at doing stuff with them. # What we do is to stuff the files into the # arrays you wanted using "push" # we push them into the array if they match # the criteria you seem to want # Note that within a match m/ / the . char has a special # meaning (it matches any single char) so to match a # literal . we "escape" it with a backslash (backwhack) my (%screenres, %thumbnail, @image4by6, @screenres, @thumbnail, @renam +edscreenres, @renamedthumbnail); # pacify strict opendir (DIR, $base_dir) or die "Unable to open dir: $base_dir Perl sa +ys: $!\n"; while (my $file = readdir DIR) { push @image4by6, $file if $file =~ m/4 by 6\.tif/; push @screenres, $file if $file =~ m/screen res\.jpg/; push @thumbnail, $file if $file =~ m/thumbnail\.jpg/; } @renamedscreenres = @screenres; foreach (@renamedscreenres) { s/ screen res.jpg$// } @renamedthumbnail = @thumbnail; foreach(@renamedthumbnail) { s/ thumbnail.jpg$// } %screenres{@renamedscreenres} = @screenres; %thumbnail{@renamedthumbnail} = @thumbnail; close DIR; # now lets print out our sorted arrays # via a function that we define (a subroutine) print "\n\nImage4by4 array contains:\n"; print_sorted( @image4by6 ); print "\n\nScreenres array contains:\n"; print_sorted( @screenres ); print "\n\nThumbnail array contains:\n"; print_sorted( @thumbnail ); print "\n\nRenamedScreenres array contains:\n"; print_sorted( @renamedscreenres ); print "\n\nRenamedThumbnail array contains:\n"; print_sorted( @renamedthumbnail ); exit; # this sub takes one argument - an array # the array is passed to the sub in the # magical @_ array. We sort this array # storing the result in @sorted # we then use a loop to iterate over # each element assigning it to $file # we print each file in turn plus a newline \n ##my $i = 1; sub print_sorted { my @sorted = sort @_; foreach my $file (@sorted) { print "$file\n";} ## for (local *IT; $screenres[$i]; ++$i) ##{ ## rename $screenres[$i], "${i}_sc.jpg"; ## rename $thumbnail[$i], "${i}_tn.jpg"; ## open IT, ">${i}_db.txt"; close IT; ## } }
Update Masem - changed title from "Seeking Wisdom...Again"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Seeking Wisdom........... Again
by srawls (Friar) on Jun 16, 2001 at 02:21 UTC | |
|
Re: Seeking Wisdom........... Again
by tachyon (Chancellor) on Jun 16, 2001 at 04:02 UTC |