in reply to A monk in training with a problem
Here is some commented code for you to perform what you were aiming for with your pseudo code. Set the $base_dir var and it will run as is.
cheers
tachyon
#!/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 = 'c:/windows'; # 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"; } closedir 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 (@image4by6, @screenres, @thumbnail); # 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\.tif/; push @thumbnail, $file if $file =~ m/thumbnail\.jpg/; } closedir 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 ); 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 sub print_sorted { my @sorted = sort @_; foreach my $file (@sorted) { print "$file\n"; } }
|
|---|