Re: sequential value check

2010-02-08 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  CS> I'm trying to figure out a way to compare a couple values to see
  CS> if they are sequential or not.  I'm running a for loop and
  CS> grabbing a value and setting a variable through each iteration. At
  CS> the end I would like to examine the results and see if they are
  CS> sequential or not.

  CS> If the values are less or more than 100 of the last then print
  CS> something like "not sequential". If they are equal or less than
  CS> 100 greater then the last then print "possibly sequential".

this sounds like an easy problem. why don't you try to code it up and
then post that code here. then we can help you with your solution.

  CS> Can anyone point me at a way to do this?

it is best to work it out for yourself and then ask for help. this is a
simple problem that shouldn't need guidance for a solution. just write
code that reflects what you said in english. if you have trouble, write
more specific english with concrete details. that is easier to translate
to code.

thanx,

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Curt Shaffer
OK. So I have tried some things. I guess the largest issue that I can't find an 
answer for elsewhere is how to evaluate variables to be >, = or <100 in one 
evaluation.

Before I get there, obviously I need to get the variables.

Here is what I am trying to do for that:

@hping_array = ();
$hcount = 1;
for (; $hcount < 5;){

system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");
chomp;
push hping_array, $_;
$hcount++;
}
print "@hping_array\n";

So the code is trying to run the hping3 command against $domain. I am awking 
for $5 which is the IPID value in the response. I am trying to push it into the 
array @hping_array. This should happen 5 times.

Then I'm printing @hping_array. I'm only getting one value and it is actually 
the whole response from hping. It seems to not respect the awk. 

#example output

HPING www.example.com (en1 xxx.xxx.xxx.xxx): S set, 40 headers + 0 data bytes
len=46 ip=xxx.xxx.xxx.xxx ttl=64 DF id=2041 sport=80 flags=SA seq=0 win=5840 
rtt=1.4 ms

I have done this partially with just doing my $hping_result = `sudo hping3 
$domain -S -p 80 -c 1|awk '{print $5}'; So I know the system command by itself 
is working.

So first question is what can help me get just the $5 into array 5 times. Then 
I can move on to evaluation of the array values.



On Feb 8, 2010, at 6:03 PM, Uri Guttman wrote:

>> "CS" == Curt Shaffer  writes:
> 
>  CS> I'm trying to figure out a way to compare a couple values to see
>  CS> if they are sequential or not.  I'm running a for loop and
>  CS> grabbing a value and setting a variable through each iteration. At
>  CS> the end I would like to examine the results and see if they are
>  CS> sequential or not.
> 
>  CS> If the values are less or more than 100 of the last then print
>  CS> something like "not sequential". If they are equal or less than
>  CS> 100 greater then the last then print "possibly sequential".
> 
> this sounds like an easy problem. why don't you try to code it up and
> then post that code here. then we can help you with your solution.
> 
>  CS> Can anyone point me at a way to do this?
> 
> it is best to work it out for yourself and then ask for help. this is a
> simple problem that shouldn't need guidance for a solution. just write
> code that reflects what you said in english. if you have trouble, write
> more specific english with concrete details. that is easier to translate
> to code.
> 
> thanx,
> 
> uri
> 
> -- 
> Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
> -  Perl Code Review , Architecture, Development, Training, Support --
> -  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Jim Gibson
On 2/8/10 Mon  Feb 8, 2010  3:55 PM, "Curt Shaffer" 
scribbled:

> OK. So I have tried some things. I guess the largest issue that I can't find
> an answer for elsewhere is how to evaluate variables to be >, = or <100 in one
> evaluation.
> 
> Before I get there, obviously I need to get the variables.
> 
> Here is what I am trying to do for that:
> 
> @hping_array = ();
> $hcount = 1;
> for (; $hcount < 5;){
> 
> system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");
> chomp;
> push hping_array, $_;
> $hcount++;
> }
> print "@hping_array\n";
> 
> So the code is trying to run the hping3 command against $domain. I am awking
> for $5 which is the IPID value in the response. I am trying to push it into
> the array @hping_array. This should happen 5 times.
> 
> Then I'm printing @hping_array. I'm only getting one value and it is actually
> the whole response from hping. It seems to not respect the awk.

The system function does not return the output of the external command to
the callsign Perl program.

See perldoc -q "output of a command"
 "Why can't I get the output of a command with system()?



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  CS> OK. So I have tried some things. I guess the largest issue that I
  CS> can't find an answer for elsewhere is how to evaluate variables to
  CS> be >, = or <100 in one evaluation.  Before I get there, obviously
  CS> I need to get the variables.

  CS> @hping_array = ();

you are not using strict and warnings. always ask perl for all the help
it can give you.

  CS> $hcount = 1;
  CS> for (; $hcount < 5;){

that is not perlish.

for ( 1 .. 5 ) {

no need for a counter since you don't even use it.

  CS> system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");

system doesn't return any output to the program, just to stdout. you
need qx or backticks to do this. and why shell out to awk when perl can
do that for you?

my $hping = `sudo hping3 $domain -S -p 80 -c 1` ;

then parse out the field value you want. i don't know hping3's format
but you seem to want the 5th white space separated field. this could be
off by one as iirc awk is 1 based on fields but perl is 0 based.

push( @hpings, (split ' ', $hping)[5] ;


  CS> chomp;

that is chomping $_ is not even set. enabling warnings would have told
you this.

  CS> push hping_array, $_;

where is the @ in that array name? that won't even compile. please make
sure your code at least compiles before posting it.

you seem to think system puts its output into $_. where did you get that
idea? 

  CS> $hcount++;

not needed as i said above

  CS> }
  CS> print "@hping_array\n";

  CS> So the code is trying to run the hping3 command against $domain. I
  CS> am awking for $5 which is the IPID value in the response. I am
  CS> trying to push it into the array @hping_array. This should happen
  CS> 5 times.

well, it doesn't. 


  CS> Then I'm printing @hping_array. I'm only getting one value and it
  CS> is actually the whole response from hping. It seems to not respect
  CS> the awk.

no, that isn't true. the system call is sending hping's output to stdout
(via awk but maybe that code is broken too. my awk is massively
rusty). your print is printing nothing (which warnings would also have
told you).

  CS> I have done this partially with just doing my $hping_result =
  CS> `sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'; So I know the
  CS> system command by itself is working.

if you know about backticks, why did you switch to system? please read
the docs and learn the difference between the two.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Curt Shaffer
Thanks Jim. I see my error now. I didn't realize you could just backtick in a 
for like that.


On Feb 8, 2010, at 7:06 PM, Jim Gibson wrote:

> On 2/8/10 Mon  Feb 8, 2010  3:55 PM, "Curt Shaffer" 
> scribbled:
> 
>> OK. So I have tried some things. I guess the largest issue that I can't find
>> an answer for elsewhere is how to evaluate variables to be >, = or <100 in 
>> one
>> evaluation.
>> 
>> Before I get there, obviously I need to get the variables.
>> 
>> Here is what I am trying to do for that:
>> 
>> @hping_array = ();
>> $hcount = 1;
>> for (; $hcount < 5;){
>> 
>>system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");
>>chomp;
>>push hping_array, $_;
>>$hcount++;
>> }
>> print "@hping_array\n";
>> 
>> So the code is trying to run the hping3 command against $domain. I am awking
>> for $5 which is the IPID value in the response. I am trying to push it into
>> the array @hping_array. This should happen 5 times.
>> 
>> Then I'm printing @hping_array. I'm only getting one value and it is actually
>> the whole response from hping. It seems to not respect the awk.
> 
> The system function does not return the output of the external command to
> the callsign Perl program.
> 
> See perldoc -q "output of a command"
> "Why can't I get the output of a command with system()?
> 
> 
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Curt Shaffer
Thanks for the clue. I have narrowed some things down. The counter is much 
nicer. I just need to get a better split I think as I'm not getting the 
grouping I would like.

On Feb 8, 2010, at 7:19 PM, Uri Guttman wrote:

>> "CS" == Curt Shaffer  writes:
> 
>  CS> OK. So I have tried some things. I guess the largest issue that I
>  CS> can't find an answer for elsewhere is how to evaluate variables to
>  CS> be >, = or <100 in one evaluation.  Before I get there, obviously
>  CS> I need to get the variables.
> 
>  CS> @hping_array = ();
> 
> you are not using strict and warnings. always ask perl for all the help
> it can give you.
> 
>  CS> $hcount = 1;
>  CS> for (; $hcount < 5;){
> 
> that is not perlish.
> 
>   for ( 1 .. 5 ) {
> 
> no need for a counter since you don't even use it.
> 
>  CS> system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");
> 
> system doesn't return any output to the program, just to stdout. you
> need qx or backticks to do this. and why shell out to awk when perl can
> do that for you?
> 
>   my $hping = `sudo hping3 $domain -S -p 80 -c 1` ;
> 
> then parse out the field value you want. i don't know hping3's format
> but you seem to want the 5th white space separated field. this could be
> off by one as iirc awk is 1 based on fields but perl is 0 based.
> 
>   push( @hpings, (split ' ', $hping)[5] ;
> 
> 
>  CS> chomp;
> 
> that is chomping $_ is not even set. enabling warnings would have told
> you this.
> 
>  CS> push hping_array, $_;
> 
> where is the @ in that array name? that won't even compile. please make
> sure your code at least compiles before posting it.
> 
> you seem to think system puts its output into $_. where did you get that
> idea? 
> 
>  CS> $hcount++;
> 
> not needed as i said above
> 
>  CS> }
>  CS> print "@hping_array\n";
> 
>  CS> So the code is trying to run the hping3 command against $domain. I
>  CS> am awking for $5 which is the IPID value in the response. I am
>  CS> trying to push it into the array @hping_array. This should happen
>  CS> 5 times.
> 
> well, it doesn't. 
> 
> 
>  CS> Then I'm printing @hping_array. I'm only getting one value and it
>  CS> is actually the whole response from hping. It seems to not respect
>  CS> the awk.
> 
> no, that isn't true. the system call is sending hping's output to stdout
> (via awk but maybe that code is broken too. my awk is massively
> rusty). your print is printing nothing (which warnings would also have
> told you).
> 
>  CS> I have done this partially with just doing my $hping_result =
>  CS> `sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'; So I know the
>  CS> system command by itself is working.
> 
> if you know about backticks, why did you switch to system? please read
> the docs and learn the difference between the two.
> 
> uri
> 
> -- 
> Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
> -  Perl Code Review , Architecture, Development, Training, Support --
> -  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Curt Shaffer
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){

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){

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";
}
}

I cannot find any reference in any documentation or Google that says I can say 
ge or le 100 or more. 

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. 

It seems like smart search might be along the right lines, but I can't find any 
similar examples.

On Feb 8, 2010, at 7:19 PM, Uri Guttman wrote:

>> "CS" == Curt Shaffer  writes:
> 
>  CS> OK. So I have tried some things. I guess the largest issue that I
>  CS> can't find an answer for elsewhere is how to evaluate variables to
>  CS> be >, = or <100 in one evaluation.  Before I get there, obviously
>  CS> I need to get the variables.
> 
>  CS> @hping_array = ();
> 
> you are not using strict and warnings. always ask perl for all the help
> it can give you.
> 
>  CS> $hcount = 1;
>  CS> for (; $hcount < 5;){
> 
> that is not perlish.
> 
>   for ( 1 .. 5 ) {
> 
> no need for a counter since you don't even use it.
> 
>  CS> system ("sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'");
> 
> system doesn't return any output to the program, just to stdout. you
> need qx or backticks to do this. and why shell out to awk when perl can
> do that for you?
> 
>   my $hping = `sudo hping3 $domain -S -p 80 -c 1` ;
> 
> then parse out the field value you want. i don't know hping3's format
> but you seem to want the 5th white space separated field. this could be
> off by one as iirc awk is 1 based on fields but perl is 0 based.
> 
>   push( @hpings, (split ' ', $hping)[5] ;
> 
> 
>  CS> chomp;
> 
> that is chomping $_ is not even set. enabling warnings would have told
> you this.
> 
>  CS> push hping_array, $_;
> 
> where is the @ in that array name? that won't even compile. please make
> sure your code at least compiles before posting it.
> 
> you seem to think system puts its output into $_. where did you get that
> idea? 
> 
>  CS> $hcount++;
> 
> not needed as i said above
> 
>  CS> }
>  CS> print "@hping_array\n";
> 
>  CS> So the code is trying to run the hping3 command against $domain. I
>  CS> am awking for $5 which is the IPID value in the response. I am
>  CS> trying to push it into the array @hping_array. This should happen
>  CS> 5 times.
> 
> well, it doesn't. 
> 
> 
>  CS> Then I'm printing @hping_array. I'm only getting one value and it
>  CS> is actually the whole response from hping. It seems to not respect
>  CS> the awk.
> 
> no, that isn't true. the system call is sending hping's output to stdout
> (via awk but maybe that code is broken too. my awk is massively
> rusty). your print is printing nothing (which warnings would also have
> told you).
> 
>  CS> I have done this partially with just doing my $hping_result =
>  CS> `sudo hping3 $domain -S -p 80 -c 1|awk '{print $5}'; So I know the
>  CS> system command by itself is working.
> 
> if you know about backticks, why did you switch to system? please read
> the docs and learn the difference between the two.
> 
> uri
> 
> -- 
> Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
> -  Perl Code Review , Architecture, Development, Training, Support --
> -  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-08 Thread Jim Gibson

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/




Re: sequential value check

2010-02-08 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  CS> #!/usr/bin/perl -w

still no warnings and strict. USE THEM. 

do it now. add them and declare all your variables. it will save your
ass.

  CS> for (1 .. 5){

  CS> my $hping = `sudo hping3 www.microsoft.com -S -p 80 -c 1`;
  CS> push @hping_array,(split'\ ',$hping)[15];

what is the \ doing there. it makes the space into a space. it is not
seen by split or the regex engine.


  CS> }
  CS> $hping_compare = "$hping_array[0]";

why the quotes? you don't need to quote something if it is a single value

  CS> foreach (@hping_array){

foreach my $ping ( @hping_array){

use named variables and not $_ whenever you can. it makes for better
code and it is easier to follow. there are cases where $_ must be used
and some places where it is good but names are better in general

  CS> On Feb 8, 2010, at 7:19 PM, Uri Guttman wrote:

stop quoting entire emails. we have those in our email folders. put your
reply BELOW the edited quoted email as needed. if you are just posting
new code with no actual reply, then why keep the useless quoted email?

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer
> URI>  still no warnings and strict. USE THEM. 
> 
> do it now. add them and declare all your variables. it will save your
> ass.
> 
I am running -w when I run the code. 
> 
> URI> what is the \ doing there. it makes the space into a space. it is not
> seen by split or the regex engine.

This is the ONLY way I can get the ID=x value. I tried both your example and 
many others. They all produced a single character, not the complete value. This 
is producing the exact result. So obviously something is using it.
> 
> URI> why the quotes? you don't need to quote something if it is a single value

Point taken, I will remove them.
> 
> 
> URI> use named variables and not $_ whenever you can. it makes for better
> code and it is easier to follow. there are cases where $_ must be used
> and some places where it is good but names are better in general

I'm getting 5 values and I need to do something different with each of them. I 
believe that is when $_ is helpful as each iteration through the loop will be a 
different value. It seems to be to be a shorter way and relatively clean to do 
in this instance. If there is a better way , please enlighten me.
> 

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer

#!/usr/bin/perl
use warnings;
use strict;
my $hping;
my $hping_compare;
my @hping_array = ();


for (1 .. 5){

$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){

print "Appears to be load balancing\n";

}
elsif ($_ ge $hping_compare){

print "Appears to be load balancing\n";
}
else{
print "Does not appear to be load balancing\n";
}
}

So now there is my new code. It all runs as expected and the with the 
suggestions of Uri. The problem still remains that I cannot find any 
documentation or search results on how one would compare as I have asked from 
the beginning. I need the ge value to be greater than by 100. If it is not 
greater than by 100, then it is a different value. 

With that in mind. Is it possible to do such a comparison? I need to do such an 
evaluation on the elsif above. in pseudocode it would be:

If (the current array element is 100 or more greater than the base array 
element){

print " Appears to be load balancing\n"
}



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Steve Bertrand
Curt Shaffer wrote:
> #!/usr/bin/perl
> use warnings;
> use strict;
> my $hping;
> my $hping_compare;
> my @hping_array = ();
> 
> 
> for (1 .. 5){
> 
> $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){
> 
> print "Appears to be load balancing\n";
> 
> }

# ignoring the fact that you were advised to use named variables
# instead of $_ where possible, here is one way to do it:

> elsif ($_ ge $hping_compare){

elsif ( ( $_ + 100 ) >= $hping_compare ) {

> print "Appears to be load balancing\n";
> }
> else{
> print "Does not appear to be load balancing\n";
> }
> }

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Steve Bertrand
Steve Bertrand wrote:
> Curt Shaffer wrote:
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
>> my $hping;
>> my $hping_compare;
>> my @hping_array = ();
>>
>>
>> for (1 .. 5){
>>
>> $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){
>>
>> print "Appears to be load balancing\n";
>>
>> }
> 
> # ignoring the fact that you were advised to use named variables
> # instead of $_ where possible, here is one way to do it:
> 
>> elsif ($_ ge $hping_compare){
> 
> elsif ( ( $_ + 100 ) >= $hping_compare ) {

I think I read what you are trying to do wrong. I'll try again:

elsif ( ( $_ - $hping_compare ) >= 100 ) {

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer
>> SB> # ignoring the fact that you were advised to use named variables
>> # instead of $_ where possible, here is one way to do it:

I do not see how I can get away from using $_ because each iteration through 
the loop will be a different variable and thus a different array element. This 
is why I continue to choose to use $_ until someone can show me how it might be 
easier or cleaner to perform 5 iterations, changing the value every time 
without using it.
>> 
>>> elsif ($_ ge $hping_compare){
>> 
>> elsif ( ( $_ + 100 ) >= $hping_compare ) {

I think this is more along the lines of what I am trying to do. I am checking 
to see if the current array element is 100 or more than the base element of 
$hping_compare. The $hping_compare is the value the want to base the greater 
than or equal to off of.




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Steve Bertrand
Uri Guttman wrote:

>   CS> foreach (@hping_array){
> 
> foreach my $ping ( @hping_array){

Uri showed right above how to avoid using $_. eg instead of:

foreach ( @hping_array ) {
$_ + 10;
#...60 lines of code
print "$_\n";
}

do:

for my $ping_result ( @hping_array ) {
$ping_result + 10;
#... 60 lines of code
print "$ping_result\n";
}

You will appreciate this in longer programs, as it is very clear as to
what the variable is for, and which scope it belongs to.

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer

On Feb 9, 2010, at 10:10 AM, Steve Bertrand wrote:

> Uri Guttman wrote:
> 
>>  CS> foreach (@hping_array){
>> 
>> foreach my $ping ( @hping_array){
> 
> Uri showed right above how to avoid using $_. eg instead of:
> 
I didn't read/understand that fully as to the problem at hand. I apologize. 
> 
> You will appreciate this in longer programs, as it is very clear as to
> what the variable is for, and which scope it belongs to.
> 
> Steve
Thanks for the input Steve!

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  URI> still no warnings and strict. USE THEM. 
  >> 
  >> do it now. add them and declare all your variables. it will save your
  >> ass.
  >> 
  CS> I am running -w when I run the code. 
  >> 
  URI> what is the \ doing there. it makes the space into a space. it is not
  >> seen by split or the regex engine.

  CS> This is the ONLY way I can get the ID=x value. I tried both your
  CS> example and many others. They all produced a single character, not
  CS> the complete value. This is producing the exact result. So
  CS> obviously something is using it.

post the output line from that command. do not let your emailer mung it
or word wrap it. show the part you want to extract out. there may be
easier ways to get it with a regex and not with split.

  URI> use named variables and not $_ whenever you can. it makes for better
  >> code and it is easier to follow. there are cases where $_ must be used
  >> and some places where it is good but names are better in general

  CS> I'm getting 5 values and I need to do something different with
  CS> each of them. I believe that is when $_ is helpful as each
  CS> iteration through the loop will be a different value. It seems to
  CS> be to be a shorter way and relatively clean to do in this
  CS> instance. If there is a better way , please enlighten me.

no, you are missing the point. the loop variable needs a name. all loop
variables are reused, hence the need of a loop!! but what kind of value
is stored there in $_? we can't tell quickly without analyzing the loop
code and seeing where the data comes from. whereas with a properly named
loop variable, we know what it is all the time inside the loop.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  CS> #!/usr/bin/perl
  CS> use warnings;
  CS> use strict;
  CS> my $hping;
  CS> my $hping_compare;
  CS> my @hping_array = ();

no need for the = () as all arrays are created empty.

  CS> for (1 .. 5){

  CS> $hping = `sudo hping3 www.microsoft.com -S -p 80 -c 1`;
  CS> push @hping_array,(split'\ ',$hping)[15];
  CS> }
  CS> $hping_compare = $hping_array[0];

  CS> foreach (@hping_array){
  CS> if ($_ le $hping_compare){

someone told you that le is wrong for numeric comparison. and WHAT do
you think is in $_ there? you never explicitly set it. it may have some
value from the loop index but that is meaningless. think about your data
when you use it. was it set properly? what should it contain? print it
to make sure.

  CS> So now there is my new code. It all runs as expected and the with
  CS> the suggestions of Uri. The problem still remains that I cannot
  CS> find any documentation or search results on how one would compare
  CS> as I have asked from the beginning. I need the ge value to be
  CS> greater than by 100. If it is not greater than by 100, then it is
  CS> a different value.

ge is for string compare. >= is for numeric.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer
> 
> 
> Uri> no need for the = () as all arrays are created empty.

I wasn't sure if strict would bark or not, so I figured better safe than sorry.
> 
> Uri> someone told you that le is wrong for numeric comparison. and WHAT do
> you think is in $_ there? you never explicitly set it. it may have some
> value from the loop index but that is meaningless. think about your data
> when you use it. was it set properly? what should it contain? print it
> to make sure.

I contains data and numerics id=, that is why I'm using them. I did print 
it before looking to compare as I normally make habit of and it is printing the 
value as expected.
> 
> Uri> ge is for string compare. >= is for numeric.

See above comment


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Curt Shaffer

> 
> 
> 
> Uri> post the output line from that command. do not let your emailer mung it
> or word wrap it. show the part you want to extract out. there may be
> easier ways to get it with a regex and not with split.

I think you may be right. I would like to pull the numerics out from the id= 
section. 

Sample output:

#begin code output

HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data bytes
len=46 ip=207.46.19.190 ttl=64 DF id=21409 sport=80 flags=SA seq=0 win=5840 
rtt=102.8 ms
 HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data bytes
len=46 ip=207.46.19.190 ttl=64 DF id=21804 sport=80 flags=SA seq=0 win=5840 
rtt=1.5 ms
 HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data bytes
len=46 ip=207.46.19.190 ttl=64 DF id=23882 sport=80 flags=SA seq=0 win=5840 
rtt=6.4 ms
 HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data bytes
len=46 ip=207.46.19.190 ttl=64 DF id=19047 sport=80 flags=SA seq=0 win=5840 
rtt=1.7 ms
 HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data bytes
len=46 ip=207.46.19.190 ttl=64 DF id=30842 sport=80 flags=SA seq=0 win=5840 
rtt=1.4 ms

# End code output

As seen above, what I am looking for is pulling the id= field

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Steve Bertrand
Curt Shaffer wrote:
>>
>>
>> Uri> post the output line from that command. do not let your emailer mung it
>> or word wrap it. show the part you want to extract out. there may be
>> easier ways to get it with a regex and not with split.
> 
> I think you may be right. I would like to pull the numerics out from the id= 
> section. 
> 
> Sample output:
> 
> #begin code output

Using the first example (forgive the wrap):

my $ping_result = "HPING www.microsoft.com (en1 207.46.19.190): S set,
40 headers + 0 data bytes len=46 ip=207.46.19.190 ttl=64 DF id=21409
sport=80 flags=SA seq=0 win=5840 rtt=102.8 ms";

> # As seen above, what I am looking for is pulling the id= field

$ping_result =~ m{ .* id=(\d+) }xms;

my $ping_id = $1;

print "$ping_id\n";

% ./id.pl
21409

Steve


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  Uri> post the output line from that command. do not let your emailer mung it
  >> or word wrap it. show the part you want to extract out. there may be
  >> easier ways to get it with a regex and not with split.

  CS> I think you may be right. I would like to pull the numerics out from the 
id= section. 

  CS> HPING www.microsoft.com (en1 207.46.19.190): S set, 40 headers + 0 data 
bytes
  CS> len=46 ip=207.46.19.190 ttl=64 DF id=21409 sport=80 flags=SA seq=0 
win=5840 rtt=102.8 ms

you did word wrap as i warned you not to. please be careful about
that. word wrapping means cutting/pasting your data to another program
will FAIL. you do not want to make it harder for people to help you.

that is easy to parse out (untested since i won't fix the word wrap):

my ( $ping_id ) = $hping =~ /\bid=(\d+)/ ;

regexes make that sort of data extraction so easy. your original awk on
fields now makes even less sense as it wasn't removing the id= part. awk
(and my split code) split on white space. looking for the id= part is
simpler and clearer (no counting of fields). and it is more robust as
the output line can add/delete fields (maybe based on options) and you
can still parse out id= and not count fields.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Uri Guttman
> "CS" == Curt Shaffer  writes:

  >> 
  >> 
  Uri> no need for the = () as all arrays are created empty.

  CS> I wasn't sure if strict would bark or not, so I figured better safe than 
sorry.
  >> 
  Uri> someone told you that le is wrong for numeric comparison. and WHAT do
  >> you think is in $_ there? you never explicitly set it. it may have some
  >> value from the loop index but that is meaningless. think about your data
  >> when you use it. was it set properly? what should it contain? print it
  >> to make sure.

  CS> I contains data and numerics id=, that is why I'm using
  CS> them. I did print it before looking to compare as I normally make
  CS> habit of and it is printing the value as expected.

what value did you get? your last code did not parse it out
correctly. if you got id=123 then you can't compare than as a string and
get the correct results when comparing to 100 no matter what the
operators used.
  >> 
  Uri> ge is for string compare. >= is for numeric.

  CS> See above comment

no, id=123 is a STRING. but 100 is a NUMBER. apples don't compare to
oranges. read perldoc perldata to learn how perl handles numbers and
strings and converts between them. perl does not divine a number INSIDE
a larger string and use that for a number.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sequential value check

2010-02-09 Thread Uri Guttman
> "SB" == Steve Bertrand  writes:


  SB> $ping_result =~ m{ .* id=(\d+) }xms;

that will match 'grid=123' or 'foo=34 noid=123' etc. the .* is allowing
anything before the id. it may work here as no field other than id ends
in 'id' but it is a poor regex. don't use *. unless you mean to grab
something or skip in the middle. you are skipping in the beginning which
happens already. the correct way to force only 'id' to be found is to
use the word boundary pattern \b in front of id. then it will be found
uniquely and no need for the .*.

also the /ms options are useless. i know you got that from PBP but i
disagree with blind use of those. there are actually times when i rely
on . NOT matching a newline and i would hate to have to remove the /s
and explain that situation.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/