Thanks a lot John . I have a little question can u please dexcribe in detail what this line is doing actually unless ( $seen{ $_ }++ )
Thanks & regards in advance Anirban Adhikary On Mon, Aug 25, 2008 at 5:34 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: > Anirban Adhikary wrote: > >> Dear List >> I have write the The following code to check a file and print only >> distinct >> lines to a new file and skips the duplicate lines. My source file is as >> follows >> >> hello how are you? >> hello how are you? >> What language did you want to use? >> What language did you want to use? >> I am here >> You are there >> this is my first perl script >> What language did you want to use? >> >> >> ############################################################################### >> my code is as follows >> >> >> #use strict; >> #use warnings; >> >> print "Enter the Absolutepath of the file\t"; >> my $input=<STDIN>; >> chomp($input); >> >> dup($input); >> >> sub dup >> { >> my $filename=shift; >> my $count=0; >> my @arr; >> my $el; >> my $element; >> open(FH,"$filename"); >> while(<FH>) >> { >> chomp($_); >> $element = $_; >> if($count ==0) >> { >> push(@arr,$element); >> $count+=1; >> print "Count is $count\n"; >> } >> else >> { >> foreach $el(@arr) >> { >> my $len=$#arr; >> chomp($el); >> # print "$element\n"; >> #print "The length of arr is $len\n"; >> if($el == $element) >> { >> print "\t######Inside If######\t\n"; >> print $element."\n"; >> next; >> #push(@arr,$_); >> } >> >> elsif($el != $element) >> { >> push(@arr,$element); >> print "\t########Inside Elseif\n"; >> #next; >> } >> } >> } >> } >> >> ############################################################### >> The problems I am facing are >> >> 1) The code is not getting entered into elsif block >> >> 2) I am comparing between 2 strings but if I use "eq" or "ne" for >> comparison I am getting some horrible output so I am using numeric >> comparison. >> >> 3)If I am giving print the $element variable outside foreach loop it is >> getting printed but Inside foreach loop it is not showing anything. >> >> Can anybody Please suggest me where I am making the mistake? >> > > The usual way to do what you want is to use a hash: > > use strict; > use warnings; > > print "Enter the Absolutepath of the file\t"; > chomp( my $filename = <STDIN> ); > > open my $FH, '<', $filename or die "Cannot open '$filename' $!"; > > my %seen; > while ( <$FH> ) { > unless ( $seen{ $_ }++ ) { > print; > } > } > > __END__ > > > > > John > -- > Perl isn't a toolbox, but a small machine shop where you > can special-order certain sorts of tools at low cost and > in short order. -- Larry Wall > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > http://learn.perl.org/ > > >