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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.