Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to remove duplicate entries from a file then print to a new output file. The following works, but how can i how print the non-duplicated hash to a file?
while (<>){ # take file with duplicate lines chomp $_; /(\d+)$/; push @array, $1; } my %uniques; foreach my $key(@array){ $uniques{$key}++; }
thanks

Replies are listed 'Best First'.
Re: print a hash
by reneeb (Chaplain) on Jan 20, 2005 at 09:53 UTC
    TIMTOWTDI (you have not specified the required output): print the structure to a file: use Data::Dumper
    use Data::Dumper; open(my $fh,">$file") or die $!; print $fh Dumper(\%uniques); close $fh;


    print it to a file like a menu:
    sub iter { my $href = shift; my $level = shift; local $_; foreach (keys %{$href}) { if (ref($href->{$_})) { iter($href->{$_}, $level+1); } else { print ' ' x $level; print "$_\n"; } } } iter(\%unique, 0);
Re: print a hash
by holli (Abbot) on Jan 20, 2005 at 10:05 UTC
    perl -n -e "chomp; /(\d+)$/; print $1 unless $hash{$_}; $hash{$_}++;" +infile>outfile

    holli, regexed monk
      thanks for your help
Re: print a hash
by prasadbabu (Prior) on Jan 20, 2005 at 09:57 UTC

    You can try this also.

    @newarray = grep (!$uniques{$_}++, @array);

    Prasad

Re: print a hash
by Random_Walk (Prior) on Jan 20, 2005 at 14:13 UTC
    perl -pi.bak -e'$_="" if $unique{$_}++' <yourfile>

    Your original file will be moved to one with a .bak extension and the version with only unique lines will be left named as the original

    Update

    Whoops, missed the fact that the OP grabs the last digits off the line and checks the uniqueness of them, not the entire line, this may be more to your liking

    perl -pi.bak -e'$_=/(\d+)/?$1:"";$_="" if $seen{$_}++' <yourfile>

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: print a hash
by Roy Johnson (Monsignor) on Jan 20, 2005 at 16:20 UTC
    my %seen; while (<DATA>) { print "$1\n" if /(\d+)$/ and not $seen{$1}++; } __DATA__ foo 1 foo 2 bar 1 bar 3 bar 4 bar 2 bar 2 bar 1 baz 1 ex 5

    Caution: Contents may have been coded under pressure.