Re: Splitting and comparing file names
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 14 Dec 2006 09:42:29 -0800
Jake wrote:
Thanks to some help on another thread I have a perl script that will
generate a file in the following format:
hostname#val1#val2#val3.load
Are those # symbols actually part of the file name, or are you using
them to demark the different values? Giving actual sample data along
with the general format would really be much preferred...
The idea now is to read in each of the file names, generate a mean
(meanval = (val1+val2+val3)/3)
Are there *always* three values, or is this just the formula for when
there are three values? Again, sample data would be nice.
and then compare the mean between the
three systems to determine the server with the lowest average load for
the past 15 minutes. I'm at the point where I'm not sure exactly how
to manage the split
Why are you presuming the need for split?
and then be able to run equations against the
values. Here's what I have so far to pull in the file to an array.
Perhaps there is a more efficient method of gathering that information
as well...
#!/usr/bin/perl
use strict;
use warning;
my ($hostname, $val1, $val2, $val3, $filename, @filesfound);
You have a case of premature declaration. Don't do that. Declare your
variables as you need them. Reduces the possibility of name
collisions, makes the code more readable, reduces memory consumption.
chdir ("/store/admin/tools/load");
Why? Changing the current local directory is not necessary for opening
that directory for reading.
opendir (DIR, "/store/admin/tools/load")
You should use lexical file and directory handles these days, not
global barewords.
|| die "Directory not found or unable to be opened. $!\n";
Don't guess as to the problem in your hard-coded error message. That's
what $! is there for.
opendir my $DIR, '/store/admin/tools/load' or die "Could not open
directory: $!";
@filesfound = sort (grep (/\.load$/, readdir(DIR)));
This is where @filesfound should be declared...
if ( ! @filesfound) {
die ("None of the load files were found. Please verify that
the writeLoad.pl script is running properly. $!\n");
closedir(DIR);
}
else {
foreach $filename(@filesfound) {
Here is where $filename should be declared.
# This is where I'm having the major issue with my own logic...
# I probably have myself going down a bad path :p
use List::Utils qw/sum/; #should really be near the top, not here.
my @vals = $filename =~ /#(\d+)/g;
my $avg = sum(@vals) / @vals;
};
Any ideas would be great. Keep in mind that I'm not a developer and
I'm sitting down with perl books and the internet to try to figure this
out. :)
At the moment I'm trying to read through and find information for
manipulating array data and I must admit that I'm just not getting it
yet.
perldoc List::Utils
perldoc perlfunc
Paul Lalli
.
- Follow-Ups:
- Re: Splitting and comparing file names
- From: Jake
- Re: Splitting and comparing file names
- References:
- Splitting and comparing file names
- From: Jake
- Splitting and comparing file names
- Prev by Date: Re: Splitting and comparing file names
- Next by Date: Re: Splitting and comparing file names
- Previous by thread: Re: Splitting and comparing file names
- Next by thread: Re: Splitting and comparing file names
- Index(es):
Relevant Pages
|
|