in reply to Re^2: Top n Unique count from Array
in thread Top n Unique count from Array

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];

Replies are listed 'Best First'.
Re^4: Top n Unique count from Array
by cipher (Acolyte) on Mar 25, 2011 at 10:26 UTC
    Dear Anonymous Monk, I am not a programmer, I have very basic perl knowledge. I am learning perl by following tutorials online and continue to do so. This is the first time I am working on hash and was hoping someone to explain me in a better way. I already posted what I tried, but you keep on asking me the answer for my question. I know my code is not good but it works, I just wanted some simple explanation of this.
      I already posted what I tried, but you keep on asking me the answer for my question.
      No, I asked you to answer my questions; you did not answer them. This is how to learn to become a programmer, you ask yourself these questions, and try to answer them.

      If you're stuck, you speak the questions out loud and ask the empty room. Does you understand the question? Would another person understand the question? Does the answer make sense?

      If you're still stuck, you ask on perlmonks, and when monks ask you questions, you try to answer their questions.

      This is how you learn to become a programmer.

      I know my code is not good but it works, I just wanted some simple explanation of this.
      Well, you asked to acheive similar output but from array not an explanation; did you want to ask for an explanation of something?

      In this reply you say UPDATE: Looks like I found the solution. This one is working for me: , but the code is exactly what you had in the beginning, you just changed the variable name, so are you sure its the solution?

        You are right, Is there any way I can get the Top results in a Hash ?
        print "$key\t$cnt_hash{$key}\n"; Is there any way that output of this print command can be pushed in an array or hash ?
        So next time when I print that hash, It will print the same output.
        I want to insert print "$key\t$cnt_hash{$key}\n"; in Tk entry widget.