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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |