Hello there, i am new on perl and i have this code :

############################## #!/usr/bin/perl -w #call of CPAN use warnings; use strict; use Cwd; #variables my $i=0; my $directory= getcwd; my $file="options"; #opening output file and adding the header on first row open(FILE, ">>OLTP.txt") or die ("Could not create file OLTP.txt") +; print FILE "User script,Serveur Name,Instance Name,Date of script, +Serveur Name2,Instance Name2,ADVANCED_COMPRESSION~HEADER,TABLE_COMPRE +SSION~HEADER,2,count,DATA_DICTIONARY_VIEW,TABLE_OWNER,TABLE_NAME,PART +ITION_NAME,COMPRESSION,COMPRESS_FOR,\n"; close FILE; #loop while files are found foreach my $files ( list_files( $directory, 1,$file ) ) { print "File : $files\n"; singlefile($files,$file); } #recursion and list integration sub list_files { my ( $directory, $recurse,$file ) = @_; require File::Spec; # Search in subdirectory or not if ( ( not defined $recurse ) || ( $recurse != 1 ) ) { $recurs +e = 0; } # verification directory if ( not defined $directory ) { die "No named directory\n"; } # Opening a directory opendir my $fh_rep, $directory or die "Can not open directory +$directory\n"; # List files and directories, except (. and ..) my @fic_rep = grep { !/^\.\.?$/ } readdir $fh_rep; # Closing directory closedir $fh_rep or die "Unable to close directory $directory\ +n"; #fill list with found files my @files; #file or folder? if file: add files to the table. if record: s +tart the recursion foreach my $nom (@fic_rep) { my $mycurrentfile = File::Spec->catdir( $directory, $nom ) +; if ( -f $mycurrentfile and $mycurrentfile=~ m/$file/ and + $mycurrentfile =~ m/\.csv$/i){ push( @files, $mycurrentfile ); } elsif ( -d $mycurrentfile and $recurse == 1 ) { push( @files, list_files($mycurrentfile, $recurse,$fil +e) ); # recursion } } return @files; } ##merge data after filtering sub singlefile { my ( $file,$out) = @_; #open file open(FILE, $file) or die ("error occured while opening file"); #create list from file my @save = <FILE>; close(FILE); #empty table rows which do not meet criteria foreach (@save){ $_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP +/ ); $_ = "" if ($_ =~m/SYSMAN/|| m/SYS/); chomp $_; } #open output file, add data, close open(FILE, ">>OLTP.txt") or die ("error occured while opening +file OLTP.txt"); foreach (@save){ print FILE $_."\n" if ($_); } close(FILE); }

the code seems to do the following :
- create txt with header
- create list of files based on every files that match with criteria (option, csv)
- for each files of the list, fill with all rows and then remove what do not match with the criteria "unless" and "if"
- push everything into the file with header (oltp.txt)

My Goal :
- create txt with header
- create list of file based on every files that match with criteria (option, csv)
- for each files of the list, **fill only with the First rows that match with the criteria "unless" and "if"**
- push everything in the file with header (oltp.txt). the final result should be the txt with header and then, only 1 line per files (if the criteria match).
many thank for your help
OliverR


In reply to how to modify foreach by OliverR

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.