I want to acheive similar output but from array.

Why?

I want to use the IP address multiple time for various purpose in my script so I am pushing them in an array.

The question was why do you want an array?

Tried this, following an example but output is not as I want it. Output is not sorted and where do I specify that I only want top n unique values.

But why did you try that?

Why should that code produce something sorted?

What series of steps does the code perform to produce a sorted array of IPs and hits?

Also, what do you mean where?

Please think about it, and try to answer these questions out loud ; speak the answer to your monitor or an object on your desk.

What I would do is redirect the output of the first program to a file, then execute head --lines=N file to get the top N results

This is one approach, but I doubt its easier to understand than using "head --lines=N file"
my %Uniq; while( my $Ip = NextIp() ){ $Uniq{$Ip}++ } my @TopDown = sort { $Fun{$b} <=> $Fun{$a} } keys %Uniq; my @TopTen = @TopDown[0..9]; # @TopTen = ( sort { $Fun{$b} <=> $Fun{$a} } keys %Uniq )[ 0 .. 9];
or
my @Top = sort { $b->[1] <=> $a->[1] } map { [ $_, $Uniq{$_} ] } keys +%Uniq; undef %Uniq; @Top = @Top[0..9];

In reply to Re^3: Top n Unique count from Array by Anonymous Monk
in thread Top n Unique count from Array by cipher

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.