-2

I would like to use

 myscript.pl targetfolder/* > result.csv

to grep some number from multiple ASCII files.

The data table is like

| 44.2 | 3123.7 | 3123 |
+--------+--------+--------+

--> this is the end of data table is like

myscript.pl

#!/usr/bin/env perl

use warnings;
use strict;
use Data::Dumper;  # for debugging

$Data::Dumper::Useqq=1;

#####start######

Title1();
Title2();

print "\n"; 

#####Grep#######

foreach my $currentfile (@ARGV) {     # ARGV is the target files list
    print Dumper($currentfile);       # debug
    open my $filehanlder, '<', $currentfile or die "$currentfile: $!";   

    while ($r <= $#fswf) {      #judge end of the open file
        Value1();
        Value2();
        Print1();
        Print2();
        print "\n";             
        $r++;
    }                 #go next line output

    Close $filehanlder; 
}

#####sub#######
sub Title1 {
    print "title1,title2";
}

sub Title2{
    print "title5,title6,title7,title8";
}


sub Value1 {
    my ($line);
    while ($line = <$filehanlder>)) {
        if ($line =~ /^\|\sMachine\:(\S+)\s+Release\:(\S+)\s+/) {
            our ($machine) = $1;our ($sw) = $2;
        }
    }
}


sub Value2 {
    my ($line);
    while ($line = <$filehanlder>)) {
        if ($line =~ /^\|\sProduction\sResult\s+\|\s(\S+)\s+\|/) {
            next if 1..4;
            my (@b) = "";
            $r = 1
            @result1 = @result2 = @result3 = @result4 = "";

            while ($line !~ /\+\-/) {                                 
                chomp $line; 
                @b = split / *\| */, $line;
                our ($result1[$r]) = $b[1];
                our ($result2[$r]) = $b[2];
                our ($result3[$r]) = $b[3];
                our ($result4[$r]) = $b[4];
                $r++;
                $line = (<$filehanlder>);
                @b = "";
            }
        }
    }
}

##I need a value as file counter, but not sure where to put it.

Sub Print1 {
    print "$machine,$sw,";   # this keeps same cross lines from same file
}

Sub Print2 {
    print "$result1[$r],$result2[$r],$result3[$r],$result4[$r],";  # change every line    
}

#####sub#######

I don't know is this correct way to pass the $filehander to the subroutine and pass it throught different subroutine.

@Dave Cross: Thanks for pointing out. Exactly as you said. If I do loop in the subroutine, then one subroutine will go to the end of file, other subroutine get nothing. So shall I do while loop in the main ? Or shall I do open in every subroutines? so I can reset the filehandler to the 1st line of the file in every subroutine. If I have multiple @result as I grep in the sub values2 , how I can print them with the max lines number of these @result. For example, I have @result5[7] ,@result6[12], so I would like to print 12 lines record, the first 7 lines with result5 grep result, the last 5 line ,result5 column keeps empty and result6 column continue printout.

seedof
  • 11
  • 3
  • 1
    Your code does not compile, eg. your `fswf` variable doesn't exist. Please make sure the code in your question is a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – elcaro Jul 13 '18 at 10:45
  • 3
    I've added some sensible indentation to your code. You're welcome, of course, but please do it yourself next time. Proper indentation is a powerful tool for understanding code and if you're asking a large group of strangers to read and understand your code, it's only polite to make that as easy as possible for them. – Dave Cross Jul 13 '18 at 11:03
  • @Dave Cross : Thank for correcting me, I am sorry the original one is too long to put here. Besides,it does not work either. I will modify it and my post later. – seedof Jul 14 '18 at 00:49
  • By the way, it's *handle*, not "hander", "hanlder", or "handler". – melpomene Jul 14 '18 at 01:46
  • @seedof: It's ok not to show us your actual code. It's not ok to show us code that doesn't even compile. Cut down your original code to a simple version that demonstrates the problem. Also, show us a cut-down version of the input and tell us exactly what output you want to get. We want to help you, but there's really not much we can do with your existing question. – Dave Cross Jul 14 '18 at 06:02

1 Answers1

1

Your filehandle is just stored in a scalar variable ($filehanlder) so it can be passed into a subroutine in exactly the same way as any other variable.

some_subroutine($filehanlder);

And, inside the subroutine:

sub some_subroutine {
  my ($fh) = @_;

  # do something with $fh
}

But I think you have more serious problems to worry about. You have two subroutines that have a while (<$filehanlder>) loop in them. The first of those to be called, will go to the end of the file, leaving the second with no data to process.

You probably want to rethink the design of this code.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97