Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

how to modify foreach

by OliverR (Initiate)
on Jun 07, 2018 at 08:44 UTC ( [id://1216085]=perlquestion: print w/replies, xml ) Need Help??

OliverR has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: how to modify foreach
by hippo (Bishop) on Jun 07, 2018 at 09:01 UTC

    If I understand both you and the code correctly then you just want to keep one (the first) matched line from @save:

    my $keep = ''; foreach (@save){ $_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP +/ ); $_ = "" if ($_ =~m/SYSMAN/|| m/SYS/); if ($_) { $keep = $_; last; } } open(FILE, ">>OLTP.txt") or die ("error occured while opening +file OLTP.txt"); print FILE $keep if $keep; close(FILE);

    Obviously untested since we don't have your data. You might be better off refactoring the script but hopefully this will get you started. HTH.

      Hello Hippo and many thanks for your help. here is what i have done : replace

      ##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); }

      by

      ##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 my $keep = ''; foreach (@save){ $_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP +/ ); $_ = "" if ($_ =~m/SYSMAN/|| m/SYS/); if ($_) { $keep = $_; last; } } open(FILE, ">>OLTP.txt") or die ("error occured while opening +file OLTP.txt"); print FILE $keep if $keep; close(FILE); }

      and the result is that i got only a file with only the header "User script,Serveur Name,Instance Name,Date of script,Serveur Name2,Instance.... and nothing else. if you want to give me a mail, i can send you some data to check. thank again OliverR

        It looks like you may have copy/pasted the line contituation character +

        $_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP +/ );

        Should be one line

                    $_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP/ );

        Have a read of File::Find

        poj
        if you want to give me a mail, i can send you some data to check.

        That's not how we do things here. :-)

        Have a read of both SSCCE and How to ask better questions using Test::More and sample data. If you can provide your code and a small set of data along those lines I've no doubt that we'll be able to see where you are going wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1216085]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found