FAQ
Hi

I'm almost totally new to Perl! :-)
I need to write a program that search a CDR file for duplicate lines and
then delete them.

This is what I have found out by reading in the Perl documentation and
this newsgroup, but I need a little more help.


I found this program in this newsgroup:

---------------
#!/usr/bin/perl

my $in_file = '/path/my_in_file';

open my $in, '<', $in_file or die "Cannot open '$in_file' $!";
open my $out, '>', 'scripted_out_file' or die "Failed to script file
for duplicates $!";

my %hash;

while ( <$in> ) {
my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
print $out $_ unless $hash{ $key }++;
}

close $out;
close $in;
---------------

This program should take out the cells 2, 3, 6 and 7 in a file split by
comma. My CDR file is split by space.

How can I use 'readline EXPR'?
Any help?... it's probably pretty simpel :-)

..//Henrik

Search Discussions

  • Jeff Pang at Dec 10, 2007 at 2:09 pm

    On Dec 10, 2007 6:58 PM, Henrik Nielsen wrote:
    Hi

    I'm almost totally new to Perl! :-)
    I need to write a program that search a CDR file for duplicate lines and
    then delete them.

    This is what I have found out by reading in the Perl documentation and
    this newsgroup, but I need a little more help.
    Follow the script you provided, you just need to change two lines for
    your new requirement.

    change:
    my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
    print $out $_ unless $hash{ $key }++;
    to:

    print $out $_ unless $hash{$_}++;


    Good luck.
  • Henrik Nielsen at Dec 10, 2007 at 2:32 pm

    Jeff Pang wrote:
    change:
    my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
    print $out $_ unless $hash{ $key }++;
    to:

    print $out $_ unless $hash{$_}++;


    Good luck.
    How simple! It works :-)
    Thx!

    Next step to work with file names and dates.

    ../Henrik
  • Dr.Ruud at Dec 10, 2007 at 7:10 pm

    Henrik Nielsen schreef:

    I need to write a program that search a CDR file for duplicate lines
    and then delete them.
    [...]
    while ( <$in> ) {
    my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
    print $out $_ unless $hash{ $key }++;
    }
    [...]
    ---------------
    This program should take out the cells 2, 3, 6 and 7 in a file split
    by comma. My CDR file is split by space.
    For SP separators specifically, use:

    my $key = join ' ', ( split / +/)[ 2, 3, 6, 7 ];


    Often this is better:

    my $key = join ' ', ( split ' ')[ 2, 3, 6, 7 ];


    (a ' ' with split works almost like /\s+/, see `perldoc -f split` about
    the details)

    How can I use 'readline EXPR'?
    The "<$in>" is a readline().

    --
    Affijn, Ruud

    "Gewoon is een tijger."

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedDec 10, '07 at 10:58a
activeDec 10, '07 at 7:10p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase