If you know the exact context of the data you want (which is a pretty reasonable assumption for ping's summary info), you can use a
m// match to extract the info you want. In my opinion, using split is the wrong way to do this.
#!/usr/bin/perl
my $host = 'dslreports.com';
my $ping_data = `ping -c4 $host`;
my ($sent, $rev, $lost, $min, $max, $avg);
if ( $ping_data =~
/(\d+) packets transmitted, (\d+) packets received, (\d+)%/
+ )
{
($sent, $recv, $lost) = ($1, $2, $3);
print "\$sent=$sent, \$recv=$recv, \$lost=$lost\n";
}
if ( $ping_data =~
m{round-trip min/avg/max = ([0-9\.]+)/([0-9\.]+)/([0-9\.]+)
+} )
{
($min, $avg, $max) = ($1, $2, $3);
print "\$min=$min, \$avg=$avg, \$max=$max\n";
}
## do what you will with those variables now...
On my system, this outputs:
$sent=4, $recv=4, $lost=0
$min=47.0, $avg=47.6, $max=48.4
You should probably read
this section of perlop pertaining to the
m// operator.
PS: I admit those long regexes could be broken up, simplified, /x'ed, etc, but hopefully you get the point! Update: no more wrapping within the <code>, at least under my settings.
blokhead
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.