Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: uninitialized values

by Random_Walk (Prior)
on Aug 19, 2013 at 14:09 UTC ( [id://1050039]=note: print w/replies, xml ) Need Help??


in reply to uninitialized values

You perform a test to see if you matched the line with PING, but then perform the print on every cycle. Is this script more like what you want?

#!/usr/bin/perl use strict; use warnings; my $count = 1; my $interval = 5; my $pkt_size = 128; my $timeout = 5; my $phost = 'www.google.com'; open CMD, "ping -c $count -i $interval -s $pkt_size -w $timeout $phost + |" or die "Can't open ping: $!"; while (<CMD>) { # look for the line like: PING www.google.com (10.160.83.59): 56 d +ata bytes next unless /PING\s+(\S+)\s+\(([\d.]+)\)/; print "host: $1\n"; print "IP: $2\n"; }

If you want to keep the values outside the loop: define the variable before the loop, then put the host/ip into the variables when you find them. Print at your leisure.

#!/usr/bin/perl use strict; use warnings; my $count = 1; my $interval = 5; my $pkt_size = 128; my $timeout = 5; my $phost = 'www.google.com'; open CMD, "ping -c $count -i $interval -s $pkt_size -w $timeout $phost + |" or die "Can't open ping: $!"; my $host; my $ip; while (<CMD>) { # look for the line like: PING www.google.com (10.160.83.59): 56 d +ata bytes next unless /PING\s+(\S+)\s+\(([\d.]+)\)/; $host = $1; $ip = $2; } print "host: $host has IP: $ip\n";

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: uninitialized values
by gaurav (Sexton) on Aug 20, 2013 at 04:15 UTC

    Thanks Random_Walk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1050039]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found