Having read your file into @array1, your code proceeds:

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: }
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.

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.


In reply to Re: doubt in perl by gone2015
in thread doubt in perl by sanku

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.