Hello, perlnewbie012215, welcome to the Monastery.

First of all, start using use strict; use warnings; This would help you with the mistake of storing localtime in $datestring and trying to print $date.

Your biggest problem is the fact that you are splitting the log line into words (roughly), and you are counting that the server1.prod.com will be the sixth word. You already know what happens if some additional parameters show up before the server name - the count is off. But what happens when someone switches the order of parameters? Try using a regular expression to match the specific parameters, and not counting which word is the server name.

UPDATE:

Take a look at this small script operating on your sample data. This should hopefully set you on the right track.

#!/usr/bin/perl use warnings; use strict; while(<DATA>) { chomp; my $servname = '[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+'; /-S.+?($servname).+-D.+?($servname).+-C.+?($servname).+$/; print "Server after -S: $1; Server after -D: $2; Server after -C: +$3;\n"; } __DATA__ 18:06:04.616 79952.57692 <2> logparams: -S server1.prod.com -D server2 +.prod.com -C server3.prod.com -X -s 1421467204 -e 1421467204 -R E:\Pr +ogram Files\725635\logs\log.tmp.chg -K -t 13 -M -k -L E:\Program File +s\725635\logs\infod.tmp.log -W -f E:\Program Files\725635\logs\after. +tmp 17:49:34.216 58140.81564 <2> logparams: -S -l -a -k server1.prod.com - +D client10.prod.com -C sql12.prod.com -X -s 1421467205 -e 1421467205 +-R E:\Program Files\561234\logs\log.tmp.chg -K -t 13 -M -k -L E:\Prog +ram Files\561234\infod10.tmp.log -W -f E:\Program Files\561234\logs\a +fter.tmp

The output:

Server after -S: server1.prod.com; Server after -D: server2.prod.com; +Server after -C: server3.prod.com; Server after -S: server1.prod.com; Server after -D: client10.prod.com; + Server after -C: sql12.prod.com;

- Luke


In reply to Re: help with splitting and printing the array by blindluke
in thread help with splitting and printing the array by perlnewbie012215

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.