in reply to doubt in perl
Having read your file into @array1, your code proceeds:
so after line 2, you have a hash whose keys are the input lines and whose values are the count of times each appeared. So far so good.1: my %s = (); 2: $s{$_}++ for @array1; 3: for my $value ( keys %s ) { 4: print "\n".$s{$value}."\n"; 5: if ($s{$value} > 1){ 6: $v=$value; 7: } 8: if ($value eq $v){ 9: delete $s{$value}; 10: } 11: else { 12: print "$value"; 13: } 14: }
You then loop through the keys, and presumably want to print out the lines with count == 1. I'm not sure why you chose this particular method for identifying those lines, but I can see that when the count == 1 then $value ne $v... because when count > 1 lines 5-7 arrange $v, which otherwise will be undefined or the last $value which had a count == 1 (and that definitely won't be the same as the current $value). However, I'd be happier if $v had been initialised. The delete on line 9 appears redundant... what's worse, you are removing entries from the hash in the middle of the loop which is working its way through the hash -- I cannot tell you whether that works, or not. I suggest that lines 5..13 can be simplified !
I wonder if you are disappointed about the order in which the unique lines are printed out ? If so, I suggest a read of keys which may clear up the mystery. But do not dispair: consider what you have in @array1...
For completeness: make friends with use strict and use warnings -- you won't regret it.
|
|---|