Several comments.

First, use something like Perl::Tidy to fix the indentation and formatting. It'll be easier to read that way. (I know that copying and pasting code in here sometimes changes the formatting, but it's a good habit anyway.)

Second, use strict and warnings. They can help prevent making silly mistakes and their warnings can help you to think about what you're doing and why. In particular, you use undeclared global variables and you use array slices in scalar context (which is rarely what you mean).

Third, use lexical filehandles and three-argument open. This helps avoid further global variables and avoids a potential problem (though not in this case) where untrusted input can change how your program performs IO.

Fourth, embrace Perlishness. For example, assigning to @names and then extracting elements into named variables directly is much easier if you assign to a list of those variables from the split in a single step. Likewise string interpolation is often clearer than lots of small concatenations. Similarly there's no reason to assign initial empty values to variables if you declare them as lexicals.

Once you've done that, you can reduce the busy work—I'm sure many of those substitutions are superfluous, and there's no reason to push values onto @keys if you're going to treat it as a hash anyway; make it a hash to start.

If you don't already have a good Perl 5 book, you might like to read the draft of my Modern Perl.


In reply to Re: Code Critique by chromatic
in thread Code Critique by rhiridflaidd

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.