Input file (i.e happy.txt generated from happy)
NAME PEND RUN SUSP JLIM JLIMR RATE HAPPY
akawle 8 20 0 100 20:100 67980 71%
akumar6 16 0 0 100 100 0 0%
apatil2 2 4 0 100 10:100 20398 67%
ashetty 0 3 0 100 40:100 9725 100%
bdash 2 0 0 100 100 0 0%
code
gen_ls_data();
sub gen_ls_data{
my($lines) = @_;
my $header_found = 0;
my @headers = ();
my $row_count = 0;
my %row_data = ();
$lines = `happy`;
system("happy > happy.txt");
my $filename = 'happy.txt';
open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
print $fh $lines ;
close $fh;
foreach (split("\n",$lines)) {
if (/NAME\s*PEND/) {
$header_found = 1;
@headers =split;
next;
}
if ( $header_found == 0 ) { }
else {
if (/^\s*$/) {
$header_found=0;
next;
}
$row_data{$row_count++} = $_;
}
}
How can I pass the happy.txt directly into the foreach loop without passing the $lines variables Linux command?
$filenamefor reading ('<'), and then you print to it? I'm not following your intentions here at all.$linesfrom arguments. (2) But you also get$linesas output ofhappy(3) Then you also open'happy.txt'to read those same lines, yet again ... (4) but then you write to that file. If you need$linesforforeach, either take them as function argument (and don't worry about files), or get them by executinghappyunder backticks, or executehappyviasystemand redirect them to a file, which you then read. One of these.