If you are using Perl 5.10 or newer then you can use the new given/when syntax to handle selecting between multiple code paths. However, anything of that nature is likely to be syntactic sugar (which may provide a programmer and maintainer efficiency gain) rather than an runtime improvement. No tinkering with code on that level is going to make any interesting runtime performance improvement however because any trivial change in runtime speed there will be completely hidden in I/O time.

A very minor speed improvement can be made by moving %monthNos population out of the loop. Turning off warnings for the entire scope is a bad idea. Either do it locally, or better still fix the error. With those changes and using given/when your code looks like:

#!/usr/bin/perl use strict; use warnings; use 5.010; my $no = 0; my %monthNos = map {$_ => ++$no} qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov +Dec }; while (my $line = <DATA>) { chomp($line); my ($mon, $day, $time, $loghost, $prog, $remainder) = split m{:?\s+}, $line, 6; my ($user) = $remainder =~ m{user=([^,]+)}; my ($rip) = $remainder =~ m{rip=([^,]+)}; my ($op) = $remainder =~ m{\s(==|<=|\*\*|\+\+)\s}; my $yr = 2012; $remainder =~ tr/"/'/; $_ //= '' for $user, $rip; given ($op) { when ('==') {print "Do da ==\n";} when ('<=') {print "Doing <=\n";} when ('**') {print "Process **\n";} when (undef) {print "No op found\n";} default {print "Duh! Can't do $op\n";} } my $csv = sprintf q{%02d/%02d/%s %s,%s,%s,"%s",%s,%s}, $day, $monthNos{$mon}, $yr, $time, $loghost, $prog, $remainder +, $user, $rip; print "$csv\n"; } __DATA__ May 2 07:06:20 l.net exim[1234]: 2012-05-02 07:06:20 1e <= inb@it.com May 2 07:06:20 l.net exim[1234]: 2012-05-02 07:06:20 1e <= inb@it.com May 2 07:06:20 l.net exim[1235]: 2012-05-02 07:06:20 1e => pp <pp@nsd. +net> May 2 07:06:20 l.net exim[1235]: 2012-05-02 07:06:20 1e ++ pp <pp@nsd. +net>

Prints:

Doing <= 02/05/2012 07:06:20,l.net,exim[1234],"2012-05-02 07:06:20 1e <= inb@it +.com ",, Doing <= 02/05/2012 07:06:20,l.net,exim[1234],"2012-05-02 07:06:20 1e <= inb@it +.com ",, No op found 02/05/2012 07:06:20,l.net,exim[1235],"2012-05-02 07:06:20 1e => pp <pp +@nsd.net> ",, Duh! Can't do ++ 02/05/2012 07:06:20,l.net,exim[1235],"2012-05-02 07:06:20 1e ++ pp <pp +@nsd.net> ",,
True laziness is hard work

In reply to Re: Process mail logs by GrandFather
in thread Process mail logs by stevbutt

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.