in reply to finding unique items in an array, from a text file
A modified version of your code
#!/usr/bin/perl use strict; use warnings; my $file = "controls.txt"; # always(!!!) use the three argument form of open open (FH, "<", $file) or die "Can't open $file for read: $!"; my @lines; while (<FH>) { chomp; push (@lines, $_); } close(FH) or die "Cannot close $file: $!"; my %seen = (); my @uniq = (); foreach my $line ( @lines ) { unless ($seen{$line}) { #if we get here, we have not seen it before $seen{$line} = 1; push(@uniq, $line); } } print join(", ",@uniq) , "\n"; # see if it worked
controls.txt contains
1 1 2 3 3 3 3 3 5 5 6 6 7 7
which gives the following output:
1, 2, 3, 5, 6, 7
Hope this helps.
Thomas
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: finding unique items in an array, from a text file
by leocharre (Priest) on Jan 13, 2009 at 19:16 UTC | |
Re^2: finding unique items in an array, from a text file
by thomastolleson (Initiate) on Jan 13, 2009 at 19:44 UTC |