in reply to Working with a unkown number of arrays
I think you're definitely on the track, to want to generalize the code to any number of directories. The first thing to do is try to eliminate variables — or rather, variable names — which perpetuate the "one chunk of code per directory" mentality.
Below is how you might reduce it to a loop.
sub Prioritise_requests { my( $input_directory, $node ) = @_; for my $dir_num ( '1', '2', '3' ) { my $input_dir = $input_directory.$dir_num; opendir DIR, $input_dir or die "Error opening directory $input_dir + - $!"; my @files = sort { -M $a <=> -M $b } map "$input_dir/$_", grep /^Flat_file.*\.txt$/, readdir DIR; closedir DIR; foreach ( @files ) { if ( Is_the_file_mine($node, $_) and Do_I_run_the_request_now($_ +) ) { move $_, $input_directory or warn "Error moving $_ to $input_d +irectory - $!"; return; } } } }
I've also made one critical bug fix in the code above: You have to add the directory name to the file name before you get any of the file's information, e.g. -M.
Also, I made a change as to the assumed return values of the two hypothetical subroutines Is_the_file_mine and Do_I_run_the_request_now. You were assuming they'd return characters. But really, they return boolean (true/false) conditions. May as well just code for that directly.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |