CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
different results: removing duplicates

 



regex2012
User

Dec 13, 2015, 12:29 PM

Post #1 of 2 (874 views)
different results: removing duplicates Can't Post

I have a file like this:
CORP1
CORP2
CORP1
CORP1
and I wanted to remove duplicates. I tried this:

Code
open my $fh9, '<', '/tmp/list.txt' or die "unable to open file 'file' for reading : $!"; 
open my $fh8, '>', '/tmp/savedlist.txt' or die "unable to open file 'file' for writing: $!";
my @data = <$fh9>;
close $fh9;
my %unique_hash;
foreach my $x (@data) {
$unique_hash{$x} =1;
}
close $fh8;
@data = keys %unique_hash;
print "@data";

Code
I did get a result like this: 
CORP1
CORP2

I tried numerous methods, even regex to remove this last leading space. I looked in all the faqs but I couldn't remove this space.

So then I tried:

Code
open my $fh9, '<', '/tmp/activek.txt' or die "unable to open file 'file' for reading : $!"; 
open my $fh8, '>', '/tmp/pollst.txt' or die "unable to open file 'file' for writing: $!";

my %seen;
my @lines;

while (<$fh9>) {
unless ($seen{$_}++) {
push @lines, $_;
}

}
print @lines;

Code
 
CORP1
CORP2

with no spaces.
wondering why? the problem is solved, just wonder why a space in the last element of the array.


(This post was edited by regex2012 on Dec 13, 2015, 12:32 PM)


BillKSmith
Veteran

Dec 13, 2015, 9:01 PM

Post #2 of 2 (853 views)
Re: [regex2012] different results: removing duplicates [In reply to] Can't Post

The problem is the quotes in the print. When @data is interpolated into a string, the contents of the special variable $LIST_SEPARATOR ($") is inserted between the elements. The default value is a space. Your code will print as expected if you change the print to:

Code
print @data;


UPDATE: A much better approach is to use modules. Use the uniq function of List::MoreUtils to do the work and a module such as File::Slurp to do the IO.
Good Luck,
Bill

(This post was edited by BillKSmith on Dec 14, 2015, 6:47 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives