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

Well, i'm back again with the same code. I took all your ideas to heart, but i was faced with an EVEN larger problem. My file names don't match up!!!! My program works perfectly. It renames and creates the txt files i want. but what it doesn't do, is organize the files so the screen res and the thumbnail shots don't match up. Here is my current code:
#!/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
    %screenres{@renamedscreenres} = @screenres; %thumbnail{@renamedthumbnail} = @thumbnail; close DIR;
    Change those % signs to @ signs. Make that close a closedir. Aside from that:

    I don't really understand what your question is. It seems you want each type of picture to match up, meaning being in the same corrosponding array index. How do you determine if they match up? Do they each have the same name, except for the suffix? If so then sort should work.

    The 15 year old, freshman programmer,
    Stephen Rawls

Re: Seeking Wisdom........... Again
by tachyon (Chancellor) on Jun 16, 2001 at 04:02 UTC

    Gday, I like your code, it seems familiar :-) A few quick points:

    1 I don't understand what your question is. What exactly do you want to do? Many monks will not have seen A monk in training with a problem so you will need to spell things out carefully. Usually you continue to post under that node but if there has been a significant delay (ie over a day!) a new post often helps to get your problem back on the radar screen at newest nodes.

    2 When you post code the shorter the better - you can ditch all the comments. I added these for your benefit but here they just make the code look long which it is not. When I see a big chunk of code I tend to go - bzzt - failure to reduce code to short test case - jump to next node.

    3 Long variable names are descriptive but once you get too long *typos* become far more common.

    4 I presume that the commented out code at the end is trying to rename the files and write a text file. Here is how you might do that.
    <begin_standard_disclaimer>Warning totally untested, I'm just making this up as I go.</end_standard_disclaimer>

    open (FILE, ">data.txt") or die "Oops! Perl whines: $!\n"; for my $i (0..$#screenres) { rename "$base_dir/$screenres[$i]", "$base_dir/".$i."_sc.jpg" or d +ie "Rename failure, Perl moans: $!\n"; print FILE "Renamed $base_dir/$screenres[$i] to $base_dir/".$i."_ +sc.jpg\n"; # continue in the same vein as required } close FILE;

    *Important note* when you open a file for writing with > you *stomp* on the old file, ie overwrite it! If you want to append to (ie add to the end of) an existing file without erasing the existing data you use >>

    # opens data.txt for writing, *erases* old data.txt open (FILE, ">data.txt") # opens data.txt for appending, adds data to end of data.txt open (FILE, ">>data.txt")

    Hope this helps

    cheers

    tachyon