At 9:17 PM -0500 2/8/10, Curt Shaffer wrote:
Ok. So again, thanks for getting me on the right track. I am now at
my compare routine. This is where I cannot figure out how to compare
within 100.
My first instinct is to write something like the following:
#!/usr/bin/perl -w
for (1 .. 5){
I am not familiar with the hping3 program. Does it return a value for
the same host each time? If so, you might want to use the sleep
function to delay your calls to hping3.
my $hping = `sudo hping3 www.microsoft.com -S -p 80 -c 1`;
push @hping_array,(split'\ ',$hping)[15];
}
$hping_compare = "$hping_array[0]";
foreach (@hping_array){
if ($_ le $hping_compare){
If the values saved in @hping_array are numerical, you should be
using the <= and >= comparison operators. 'le' and 'ge' are for
strings.
print "Appears to be load balancing\n";
}
elsif ($_ ge $hping_compare){
print "Does not appear to be load balancing\n";
}
else{
print "Something Else\n";
There is, of course, no way for your program to get here. Either 'le'
or 'ge' must be true.
}
}
I cannot find any reference in any documentation or Google that says
I can say ge or le 100 or more.
The way to compare within 100 is to subtract the two values, take the
absolute value of the difference, and compare that result to 100.
If you know which value is greater, you don't have to take the absolute value.
If your array contains numerical values (you haven't really said),
then you can sort the array and easily get the minimum and maximum
values:
my @sorted = sort { $a <=< $b } @hping_array;
Note that the default for sort is alphabetic order using the cmp
operator, so you have to supply an explicit comparison routine using
the numerical <=> operator.
Now you can compare the minimum and maximum values:
if( ($sorted[-1] - $sorted[0]) > 100 ) {
# difference is > 100
}
If, on the other hand, you want to compare successive values, iterate
through the array:
for my $i ( 1 .. $#sorted] ) {
if( ($sorted[$i] - $sorted[$i-1]) > 100 ) {
# two successive values differ by more than 100
}
}
What I am trying to do is say if the 2, 3,4 and 5 values are less
than or equal to, then print "We are probably load balancing". Elsif
we are greater than by more than 100 print "We are probably load
balancing". Else if we are greater than but less than 100 greater
than print "we are probably not load balancing.
Please trim irrelevant material from your post. Thanks.
--
Jim Gibson
j...@gibson.org
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/