G'day Leo,

"... there is probably a better way to do it."

There would be many ways to this. The unqualified term "better" is highly subjective: if it relates to efficiency, then Benchmark possible solutions; if it relates to other criteria, you'll need to tell us about that or assess for yourself.

Here's one way you could do it:

$ perl -e ' use strict; use warnings; use autodie ":all"; my @hosts = qw{8.8.8.8 8.8.4.4}; my $command = join ";", map "ping -c3 ".$_, @hosts; my $result = qx{$command}; my $re = qr{(?s:^(\S+).+?(\S+)\s+ms\s*$)}; my $fmt = "%s min:%s avg:%s max:%s mdev:%s\n"; open my $mh, "<", \$result; { local $/ = "PING "; while (<$mh>) { chomp; next unless /$re/; printf $fmt, $1, split /\//, $2; } } ' 8.8.8.8 min:16.929 avg:17.290 max:17.585 mdev:0.272 8.8.4.4 min:15.392 avg:15.856 max:16.284 mdev:0.365

Notes:

I've shown a number of different ways to do various things. Pick and choose the bits you want; or, use as is, if you like all of it.

— Ken


In reply to Re: Create a map from multiple ping output by kcott
in thread Create a map from multiple ping output by leostereo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.