# 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 #### foreach my $key ( keys %value_of ) { print "$key, $value_of{ $key }\n"; }