One of the first things you usually want to consider, when looking a task to be done, is "what part of it can I automate?" With Perl, that often means looking for a module - and when you want to walk directories, File::Find should be the first thing that comes to mind.
There are also a number of problems in your script - trying to pass a filehandle (instead of a filename) as an argument to the script, lots of temporary variables, invoking 'system' unnecessarily and failing to check its return - as a number of other people here have pointed out. I'd usually correct those and explain how to do them better, but this isn't just a case of a poorly-written function or two - your approach is the largest problem. I'm just going to take the lazy way out and provide the solution, while hoping that you'll consider those problems from a new perspective.
Since I don't have your data set, and since much of your code is redundant and difficult to read, I'm going to take a few liberties and guess at what you're trying to do in places. You're probably going to need to correct a few things.
#!/usr/bin/perl -w use strict; use File::Find; ### User-modifiable variables ### my $reg = 'AZT,IOP,AERTC'; my $week = 'W36'; ### User-modifiable variables ### # Get the dirlist for File::Find my @dirs = glob "semaine_$week/Regulation_{$reg}"; # The 'find' subroutine comes from 'File::Find' find(\&process, @dirs); my %seen; sub process { # Skip if we've already processed the file in this directory next if $seen{$File::Find::dir}; # Skip unless the file matches the spec next unless /^slot_list\w*.\d\d\.01\.\w+\.out$/; # We've found the file that we want; now, we'll process it open Out, ">results.csv" or die "results.csv: $!\n"; open Fh, $_ or die "$_: $!\n"; while (my $line = <Fh>){ chomp $line; my ($key, $val) = split /;/, $line; print Out "$key;$val\n"; } close Fh; close Out; # Mark directory as 'processed' $seen{$File::Find::dir}++; }
Update: Corrected the regex. I'd already fixed it in the script, but still had the old copy in the buffer...
-- Human history becomes more and more a race between education and catastrophe. -- HG Wells
In reply to Re: results printing error
by oko1
in thread results printing error
by steph_bow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |