Re: Splitting and comparing file names



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

.



Relevant Pages

  • Re: hash key not found .. WHY NOT?
    ... filea.csv format: ... Where file1.csv is using BK=AK to lookup A3 and A4. ... Not a word of this makes sense nor matches your sample data. ... Please show REAL sample input and REAL sample output. ...
    (perl.beginners)
  • Size limits on IWMWriter::WriteSample ?
    ... building a profile from the stream information from the codec that mostly ... I'm using the "20kbps, 22Khz, Mono CBR" codec format. ... that is missing the end of the sample data. ...
    (microsoft.public.windowsmedia.sdk)
  • Re: Conditional Format
    ... Assuming the sample data: ... Let's split the data first ... Select "MDY" from the drop menu under "Col data format" ... Click Format button> Patterns tab> Light blue? ...
    (microsoft.public.excel.newusers)
  • Re: problems with saving double type data in a Matrix/Array
    ... which are in double format. ... I used this declaration: ... Assuming you're doing this in MATLAB, this line of code will only assign the ... Because you initialized M with the empty double array, ...
    (comp.soft-sys.matlab)
  • Re: a sql query that doesnt return any record (performing date comparison)
    ... Name: Table1 ... Congratulations on using ISO format. ... Considering that JET stores datetime data as 8 bytes that are roughly ...
    (comp.databases.ms-access)