You're using the 3-argument form of open which is good but you're not checking whether it worked which is less good. Try doing something like the following whenever you open a file:

open my $file_handle, $mode, $filename or die $!;

While it's a matter of personal style, I'd use lowercase characters for the file handle variables; leaving uppercase for global file handles such as STDOUT, DATA, etc. and constants.

Regarding the two file-related errors you questioned:

There's two issues with your regular expressions:

Here's a piece of code that demonstrates what you want:

#!perl use strict; use warnings; my %seen; while (my $line = <DATA>) { my ($key, $rest) = split /,/, $line, 2; $key =~ s{ [-&_+'] }{ }msx; $key =~ s{ ( [a-z] ) ( [A-Z] ) }{$1 $2}msx; if ($seen{$key}++) { print STDERR join '', 'DUP: ', $line; } else { print STDOUT join '', 'NEW: ', $key, ',', $rest; } } __DATA__ Group One,Captain,Phone Number,League Pos,etc. Group-One,Captain,Phone Number,League Pos,etc. GroupOne,Captain,Phone Number,League Pos,etc. Group Two,Captain,Phone Number,League Pos,etc. Group Three,Captain,Phone Number,League Pos,etc.

Here's the output:

$ del_dup_prob.pl NEW: Group One,Captain,Phone Number,League Pos,etc. DUP: Group-One,Captain,Phone Number,League Pos,etc. DUP: GroupOne,Captain,Phone Number,League Pos,etc. NEW: Group Two,Captain,Phone Number,League Pos,etc. NEW: Group Three,Captain,Phone Number,League Pos,etc.

Commenting out the second regex (which converts GroupOne to Group One in $key) the output becomes:

$ del_dup_prob.pl NEW: Group One,Captain,Phone Number,League Pos,etc. DUP: Group-One,Captain,Phone Number,League Pos,etc. NEW: GroupOne,Captain,Phone Number,League Pos,etc. NEW: Group Two,Captain,Phone Number,League Pos,etc. NEW: Group Three,Captain,Phone Number,League Pos,etc.

Obviously, you'll need to read in your input file and you won't want the NEW: and DUP: strings I added for illustrative purposes but otherwise you could run this code as:

script_name input_filename > unique_rows_filename 2> duplicate_rows_fi +lename

Finally, given the list of special characters in your regex, there's more variety to your input data than you've shown here. That's fine, but you may need to tweak the regexes I've provided.

-- Ken


In reply to Re: Remove duplicate entries by kcott
in thread Remove duplicate entries by PyrexKidd

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.