Dear Monks, I have a script that does what it should, and runs quickly, yet I have a feeling that I can accomplish what I want more quickly. Bear with me if this seems too simplistic, I have only been using perl for a couple of weeks, and I have only just started on the path to enlightenment.

I have a large text file that contains several fields in a fixed format. Among the fields are a unique numerical ID, and a text ID that ideally should be unique. The text id also should not start with a capital letter, but sometimes does.

My script reads the file, detects IDs that start with a capital letter, and also detects if any of the textual IDs are duplicated. The numerical IDs are always unique.

I find duplicates by storing a count in a hash where the text id is the key. After I have filled the hash, I then traverse it to find values higher than one. If such a value is found, I run through the entire file again to find the numerical IDs of the duplicates.

This means that I first go through the file once to detect duplicates, and then go through the file again once for each duplicate found. I can't help but think that there is a more elegant and efficient way of doing things. My code is shown below:

#!perl -w use strict; my(@lines,%dup, $id, $key); chomp(@lines=<STDIN>); # read file, put count of key in hash print "Keys with initial caps:\n"; foreach(@lines) { if (m/PROBABLECAUSE\w*\((\d+),\s*\w*,\s+(\w*)/) { ($id, $key)= ($1, $2); $dup{$key} += 1; # check if any key has init caps (not allowed) if ($key =~ m/^[A-Z]\w*/) { print "Id: $id - $key\n"; } } } # check hash for duplicates, if found, display positions in file print "Duplicated keys:\n"; foreach $key (keys %dup) { if ($dup{$key}>1) { print "$key ($dup{$key})\n"; foreach(@lines) { if (m/PROBABLECAUSE\w*\((\d+),\s*\w*,\s+($key),/) { print "Id: $1\n"; } } } }
A typical line in the data file looks like this:

PROBABLECAUSE(0, probablecauseUndefined, undefined, Unknown, indetermi +nate, prim, false, "", UNIDENTIFIED, Y)
The values I look at are the first and the third value in the argument list.

Thank you for your time.

update

Using the tips I received, the solution is now cleaner and more elegant, and as an added bonus, I have learned about perl references. Thank you, monks!


In reply to I sense there is a simpler way... by HelgeG

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.