Okie... I know this is pretty boneheaded, but I am not entirely fluent in perl, and am at the point where I know just enought to be dangerous. <grin>

Heres what I need help on:

given a file called foo.log with lines of text that look like this:
Sep 15 14:18:54 203.146.91.228:23 202.88.143.72:23

I wanted to be able to strip that seconds field from the timestamp, so that 14:18:54 would become 14:18

so far, I have gotten an array of the items in each line, and from that array, created various scalars based on what the object is... i.e. month, day, time, data1, and data2

here is the code sofar:

##!/usr/bin/perl # #my @lines; #@lines=`cat foobar`; # #foreach (@lines) { # # my($month, $day, $time, $data1, $data2) = split / /; # open(NEWTIME, $time); # while (<NEWTIME>) { # my($hour, $minute, $second) = split /:/; # $time2 = join(':', $hour, $minute); # } # select NEWLOG; # print "$time2\n"; #open NEWLOG, ">>foofinal.txt"; #}


of course ignore the hashmarks...

Now, after testing each variable, they are all assigned as they should be...except for @time2. I cant get $time2 to reflect the result of the join statement, and then print it out to the final txt file.

The goal is to have $time2 be the HH:MM time format, and reinsert that into the logfile so that in the final file the lines would look like this:

Sep 15 14:18 203.146.91.228:23 202.88.143.72:23

I know I have probably made many errors, so please show me the light!

Oh, and I did originaly have use strict in the beginning, however, that continuously gave me errors with $time2 and removing the 'use strict' line cleared those errors (of course) and allowed the script to complete. However at teh end of each run with the time variable, $time2 is always printed as a " " instead of a join between $hour and $minute.

Thanks
Jeff


In reply to get my variable out of the hole! by Bladernr

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.