in reply to Parsing many files and output to one file. Pls HELP

while (<INPUT>) { chomp($_); # Look for title line if ($_ =~/Start -/) { my @sdk_line = split(/\t/, $_); push(@temp, substr($sdk_line[1], 18)); } } while (<INPUT>) { chomp($_); # find process time for that title if ($_ =~/Process/) { print split(/\t/, $_), "\n"; my @sdk_line = split(/\t/, $_); push(@temp , $sdk_line[2]); } }

The second while loop will never be executed, because the first one exhausts all line in the INPUT file.

Also please indent your code in a way that makes it obvious to see which loop ends where.

I have hardcoded my script to parse only 3 files. How do I do it for multiple/unknown number of files.

By iterating over the file names, open each, process it, and close it again. Something like this:

for my $fn (@file_names){ open my $handle, '<', $fn or die "Can't open `$fn' for reading: $! +"; # do something with the file here # and write to output file close $fn; }

Replies are listed 'Best First'.
Re^2: Parsing many files and output to one file. Pls HELP
by hiradhu (Acolyte) on Nov 11, 2008 at 08:55 UTC
    Thanks. Those 2 while loops are from difference subs. I have done parsing of 6 files (parse to get the xml names & the Process time) and have it array. My program works fine for 6 files. Now If I have more than 6 files (n number of files..Unknown number) How do i dynamically name arrays say $myArray$i ? I can not read one file at a time and output it. The output file should be printed column wise. Each column in the output file will be from each file.
      Instead of naming them $myArray1, $myArray2 store them all in a common array. How that works is described in perllol and perlreftut, as well as in every good Perl book.