Hey PerlMonks
So I have this script and I know how to read it for a file and get the output in txt format. But I am having problems reading a whole folder containing files. I read up on opendir and readdir.. But I am not sure if the code I am using is good? I get no errors..it is running but then my output .txt file is blank.
Can someone help me out or tell me how I can read the whole folder and tell perl to read each file, parse the columns I need then read these columns line by line and output all the result in a single txt file in the end?
Thanks in advance
#!usr/bin/perl -w
use strict;
my $directory = "/Users/My_folder/electrical_records";
opendir (DIR, $directory) or die $!;
open (my $out, ">electrical_RESULT.txt");
my @files = grep {/_*_.txt/} readdir DIR;
foreach my $file (@files) {
while (my $file = readdir(DIR)) {
next if /^\s$/; # skip blank lines
my ($meter_read, $energy_consumption) = (split /\s+/)[4,7];
# energy consumption must meet min, max criteria
if ($energy_consumption =~ /^[^0-1-.]/ or ( $energy_consumption <
+60 or $energy_consumption > 120))
{
print $out " $meter_read \ $energy_consumption \ n";
}
}
closedir(DIR);
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.