That seems an awful amount of work. I haven't analyzed the code yet, but from the little information you gave, this seems to work as well and be more understandable:
sub beautify { my %temp = map { $_ => 1 } @_; return sort { $a <=> $b } keys %temp; }

Of course, if you have a large array, this may not be efficient enough - you are making several copies of the array and on very large arrays that could hurt.

Your current code works ( I think ) because:

  1. If it is a new key $new{$_} and $old{$_} will both be 1 and grep will include that key in the returned array
  2. If it isn't an new key,
    1. $new{$_} will be incremented and $old{$_} will not. Thus, for example, $new{$_} == 2 and $old{$_} == 1.
    2. Your secondary test is $new{$_}+1 == $old{$_}. Since we have just established $new{$_} > $old{$_}, it is obviously true that $new{$_} + 1 > $old{$_} and the second tertiary test will return 0.
You are working way too hard at this. Let the hash worry about enforcing unique keys.

UPDATE: I just read this again and realized the tone is very wrong. I am not trying to be rude, but I suddenly slipped into writing a math proof. mikfire


In reply to Re: quandery about a grep statement by mikfire
in thread quandery about a grep statement by jynx

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.