When you have (key, value) pairs, think of a hash. The way you gather up the data for @PORTS is fine, but I've put my variable names in lowercase.

# remove first three lines shift @ports for 1 .. 3; # remove trailing newline from each line chomp @ports; # turn the lines into key+value pairs my %value_of = map { split /:/, $_, 2 } @ports; while ( my ( $key, $value ) = each %value_of ) { print "$key,$value\n"; } __END__ Error text, Error 10061 connecting socket. No connection could be made + because the target machine actively refused it. Connection time, 985 ms Connected port, -1 Error code, 10061 Connected host, - Host, localhost

Note that I'm assuming that the "Error text" value was actually one line even though your post has it as two lines with a break in the middle of "the".

Using shift to remove three lines off the front is sort of lazy. If it were much more, I'd use splice instead. Another way to loop over the keys would be:

foreach my $key ( keys %value_of ) { print "$key, $value_of{ $key }\n"; }

Whether you use keys or each, they'll come out in no particular order.

If you're producing CSV files, you might want to look into Text::CSV rather than do that work yourself.


In reply to Re: format output from a exe within a perl script by kyle
in thread format output from a exe within a perl script by swartzieee

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.