I would look at it from a different perspective...By putting them into arrays, you lose what is associated with what, so I would look at anonymous hashes to do the trick.
#!/usr/bin/perl -w use strict; my $ip=''; my %hash=(); while(<>) { chomp; if (/\+/) { #we have an ip address...so parse it. my $temp=$_; $temp=~s/^\+\s//g; $ip=(split (/\s/,$temp))[0]; $ip=~s/\s//g; } if ($ip !~ /\d{3}\.\d{3}\.\d{1,3}\.\d{1,3}/) { #this should never happen print STDERR "Don't have an IP address...something is not righ +t here...\n"; exit (-1); } push(@{$hash{$ip}},$_); } ###########################################3 #this will print a table like #IPADDRESS=<the ip address of the server> # SERVER=<server name> # PORTNUMBER=<port number> # SERVICE=<what service is running> # DESCRIPTION=<description of service> ####################################################### foreach my $ip_address (keys %hash) { print "IPADDRESS=$ip_address\n"; my $loop_num=0; foreach my $information (@{$hash{$ip_address}}) { #here is where you parse the information... #to get the servername, etc. #print "$information\n"; if ($loop_num == 0) { #you know this is the first entry, so this will have the s +erver name my $server=(split(/\s/,$information))[-1]; print "\t\tSERVER=$server\n"; } elsif ($loop_num == 1) { #this will have the port number if ($information =~ /(\d+)/) { my $port_number=$1; print "\t\tPORTNUMBER=$port_number\n"; } my $service=(split(/\|\_\_\_/,$information))[1]; $service=join(" ",split(" ",$service)); #need to get the port number off the front... $service=~ s/^\d+//g; $service=join(" ",split(" ",$service)); print "\t\tSERVICE=$service\n"; } elsif ($loop_num == 2) { #here is your description my $description=(split(/\|\_\_\_/,$information))[1]; $description=join(" ",split(" ",$description)); print "\t\tDESCRIPTION=$description\n"; } $loop_num++; } }
I took the * off of the example you gave as well as fixed the word wrap that happened, and it gave the following results...
IPADDRESS=192.168.29.21 SERVER=orcweb.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 sandbox FTP server (Version 1.1.214.7 Thu Aug +10 09:57:38 GMT 2000) ready... IPADDRESS=192.168.31.3 SERVER=ftp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 ftp.acme.com FTP server ready... IPADDRESS=192.168.33.19 SERVER=acmeftp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220-acme Secure FTP Server.. WarFTPd 1.71.02 (Feb +14 2000) Ready.. (C)opyright 1996 - 2000 by Jarle (jgaa) Aase - all r +igh IPADDRESS=192.168.89.1 SERVER=acmeorp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] IPADDRESS=192.168.11.22 SERVER=commerce.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 commerce Microsoft FTP Service (Version 4.0).. +. IPADDRESS=192.168.19.24 SERVER=webapp.acme.com PORTNUMBER=21 SERVICE=File Transfer Protocol [Control] DESCRIPTION=220 WEBAPP1PRI Microsoft FTP Service (Version 5.0) +...
Output is not in the same order as the input, but you can fix that by sorting the hash keys. Anonymous hashes are my method of choice whenever I have to group lines/records by a unique key. I know this is different than you were asking, but doing it in this fashion makes it a little more flexible to work with since you know the order that the data will come in. cheers! krazken

In reply to Re: Splitting Across Multiple Lines by krazken
in thread Splitting Across Multiple Lines by dru145

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.