in reply to Help with my rookie logic

JavaFan has nailed the essential problems (as he usually does), but I'll add a few more (which are admittedly less important):

There's nothing inside your "while" loop that changes the value of $GetName, yet every iteration of your loop checks whether the value of $GetName contains an open paren. You could just check for this once, before going into the loop.

The inner "for" loop checks whether the numbers "1" through "12" occur in the current file name, but the way you're doing it, a file name containing "21" will match on two iterations (because it contains "1" and "2") and a name containing "1123" will match five times ("1", "2", "3", "11" and"12").

There seems to be a dependency between the value of $GetName and the values of $StartCh and $EndCh, but I'm not sure how these are related, exactly. If you can clear that up, things might make more sense.

If you are dealing with file names that use fixed-width digit strings with leading zeros, you might want to use a regex that checks for a specific number of digits between non-digit characters -- for example, if you're looking for file names in the range "test_0001.txt" through "test_0012.txt":

my $digit_width = 4; my $min = 1; my $max = 12; my $prefix = "test_"; my $ext = ".txt"; while ( my $file = readdir DIR ) { next unless ( $file =~ /^ $prefix (\d{$digit_width}) $ext $/x ); if ( $1 >= $min and $1 <= $max ) { # we have a match... now what? } }

Replies are listed 'Best First'.
Re^2: Help with my rookie logic
by rookie_monk (Novice) on Sep 17, 2010 at 20:11 UTC
    Yes there is a dependency on the $GetName value because that is what the user is going to input for the program to use as a search criteria when it scans the directory for files. The $StarCh and $EndCh are basically chapter numbers. starting and ending chapters. Each of these files that will be in the directory The files will have the chapter number within the file name somewhere. For example "Test_001.txt" will be chapter 1 file but depending on who created the files, someone could have a totally different naming convention such as "Ch 1 title 3rd edition". Something like this will throw off my whole program and thats where I need the program to be more robust. I need code to handle many possibilities. Now thinking about it, maybe there isnt a way for to actually code all possibilties. Thanks for your input and some of your suggestions does help me for what its worth. Thanks!
    Using your code: my $digit_width = 4; # This isnt necessarily true every time my $min = 1; my $max = 12; my $prefix = "test_"; my $ext = ".txt"; while ( my $file = readdir DIR ) { next unless ( $file =~ /^ $prefix (\d{$digit_width}) $ext $/x ); if ( $1 >= $min and $1 <= $max ) { # we have a match... now now we want to create the new file na +me # Whatever $SetName was so if $SetName = "Chapter \1" which me +ans # The new file name for the program to modify in the directory + is # to "Chapter 01.txt" for the 1st chapter file which wouldve b +een # Test_001.txt in the directory it searched in. I will then # have the code rename the file. } }