in reply to How to (get started on) sort AoA or AoH by frequency

I took your question to mean: how do I make a sorted printout by frequency of the structure that my code builds? First, I found the OP's code to be confusing, so I recoded it.

It is not possible to sort a hash, but it is possible to sort the keys of the hash into an array. Then use that array of keys to print the hash. Below, I used pp() to assist in the printing.

In this example, the sub hash is actually not necessary. A HoA would have sufficed because the value of the array evaluated in a scalar context is the "count". Not quite sure what you mean in terms of AoA to sort.

Update: As clarification to the OP, @all_arrays is an array of references to arrays. The map{@$_} takes each array reference and expands it into a list of numbers. So this is the answer to one of the questions about needing to know that there are 3 rows, you don't. The code below "flattens" the whole structure into a long list of numbers no matter how many rows that there are.

#!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my @all_arrays = ([1 .. 20], [10 .. 30], [19 .. 40], ); my %unique_descriptive; foreach my $num (map{@$_}@all_arrays) { $unique_descriptive{$num}{count}++; push @{$unique_descriptive{$num}{values}}, $num; } #print pp(\%unique_descriptive); # example for num=10 #10 => { count => 2, "values" => [10, 10] }, my @sorted_keys = sort{ $unique_descriptive{$a}{count} <=> $unique_des +criptive{$b}{count} or $a <=> $b }keys %unique_descriptive; foreach my $key (@sorted_keys) { printf "%2d=", $key; #make the print out look nice print pp($unique_descriptive{$key}),"\n"; } __END__
Program output:
1={ count => 1, "values" => [1] } 2={ count => 1, "values" => [2] } 3={ count => 1, "values" => [3] } 4={ count => 1, "values" => [4] } 5={ count => 1, "values" => [5] } 6={ count => 1, "values" => [6] } 7={ count => 1, "values" => [7] } 8={ count => 1, "values" => [8] } 9={ count => 1, "values" => [9] } 31={ count => 1, "values" => [31] } 32={ count => 1, "values" => [32] } 33={ count => 1, "values" => [33] } 34={ count => 1, "values" => [34] } 35={ count => 1, "values" => [35] } 36={ count => 1, "values" => [36] } 37={ count => 1, "values" => [37] } 38={ count => 1, "values" => [38] } 39={ count => 1, "values" => [39] } 40={ count => 1, "values" => [40] } 10={ count => 2, "values" => [10, 10] } 11={ count => 2, "values" => [11, 11] } 12={ count => 2, "values" => [12, 12] } 13={ count => 2, "values" => [13, 13] } 14={ count => 2, "values" => [14, 14] } 15={ count => 2, "values" => [15, 15] } 16={ count => 2, "values" => [16, 16] } 17={ count => 2, "values" => [17, 17] } 18={ count => 2, "values" => [18, 18] } 21={ count => 2, "values" => [21, 21] } 22={ count => 2, "values" => [22, 22] } 23={ count => 2, "values" => [23, 23] } 24={ count => 2, "values" => [24, 24] } 25={ count => 2, "values" => [25, 25] } 26={ count => 2, "values" => [26, 26] } 27={ count => 2, "values" => [27, 27] } 28={ count => 2, "values" => [28, 28] } 29={ count => 2, "values" => [29, 29] } 30={ count => 2, "values" => [30, 30] } 19={ count => 3, "values" => [19, 19, 19] } 20={ count => 3, "values" => [20, 20, 20] }
Update: Another set of code - probably is not what OP needs for AoA, but it does demo how to add a column and how to sort a 2-D array by different column positions...
#!/usr/bin/perl -w use strict; use 5.010; #for new //= operator use Data::Dump qw(pp); my @all_arrays = ([1 .. 20], [10 .. 30], [19 .. 40], ); my @unique_descriptive; foreach my $num (map{@$_}@all_arrays) { $unique_descriptive[$num]++; #simple peg counter } # add a column to the 2-D array with row number # undef counts as freq of zero, the //=0 does that my $i=0; @unique_descriptive = map{[$i++,$_//=0]}@unique_descriptive; @unique_descriptive = sort{ $a->[1] <=> $b->[1] #by freq or $a->[0] <=> $b->[0] #by peg number }@unique_descriptive; foreach my $row (@unique_descriptive) { print "num = $row->[0] \tfreq=$row->[1]\n" if ($row->[1] > 0); }
AoA output:
__END__ num = 1 freq=1 num = 2 freq=1 num = 3 freq=1 num = 4 freq=1 num = 5 freq=1 num = 6 freq=1 num = 7 freq=1 num = 8 freq=1 num = 9 freq=1 num = 31 freq=1 num = 32 freq=1 num = 33 freq=1 num = 34 freq=1 num = 35 freq=1 num = 36 freq=1 num = 37 freq=1 num = 38 freq=1 num = 39 freq=1 num = 40 freq=1 num = 10 freq=2 num = 11 freq=2 num = 12 freq=2 num = 13 freq=2 num = 14 freq=2 num = 15 freq=2 num = 16 freq=2 num = 17 freq=2 num = 18 freq=2 num = 21 freq=2 num = 22 freq=2 num = 23 freq=2 num = 24 freq=2 num = 25 freq=2 num = 26 freq=2 num = 27 freq=2 num = 28 freq=2 num = 29 freq=2 num = 30 freq=2 num = 19 freq=3 num = 20 freq=3

Replies are listed 'Best First'.
Re^2: How to (get started on) sort AoA or AoH by frequency
by jonc (Beadle) on Jun 13, 2011 at 14:37 UTC

    Whew... This is intense.

    I'm sorry, but I've tried, and read some articles on hashes/data structures, but still need some assistance understanding this.

    I get the map statement. But:

    $unique_descriptive{$num}{count}++; push @{$unique_descriptive{$num}{values}}, $num;
    Is a little confusing.

    Here's what I got so far: The first statement increments the value of "count"(which I guess is a new key made then and there?). The value is in the HoH %unique_descriptive, at the key: That is the number, which is the element of the de-referenced array being looped through.

    Then the 2nd line is AoHoH?? But that array is never used later? The keys of the most inner hash are the values of something(what?). The end value of this is the number from the loop being pushed in. The 2nd inner hash is at the key of $num. Was the @ in front only necessary b/c push takes list context?

    Then the other problem is:

    my @sorted_keys = sort{ $unique_descriptive{$a}{count} <=> $unique_des +criptive{$b}{count} or $a <=> $b }keys %unique_descriptive;

    The numbers are being sorted based on count first (did you know to put $a where it is b/c $num was there before?) And if that is equal, the numbers themselves are compared. The keys are being sorted.

    Sorry for the trouble I'm having with this, I hope I was close/this makes sense to you.

      The first statement increments the value of "count"(which I guess is a new key made then and there?).

      Yes. Perl will "autovivify" a new entry if none already exists. The is pretty cool stuff. In other languages I would have had to size and initialize the structure. In Perl, I can just do that as I go.

      But that array is never used later?

      Correct. My code produces exactly the same structure as your code, but in more understandable way (at least for me!). The "values" really aren't needed (and yes this is an @array). I just did that because your code did it. The "values" will always be the same as the number key and always repeated the same times as the frequency. That could easily be computed. So really all that is needed is a single dimensional hash instead of two dimensions (see my code later in the thread).

      update:
      I'm not sure about your understanding of sort... Trying to further clarify... $a and $b are two hash keys that sort chooses for us - these are some chosen set of number pairs. We don't have to be concerned with the algorithm that sort uses, we just have to tell it how to compare a and b.

Re^2: How to (get started on) sort AoA or AoH by frequency
by jonc (Beadle) on Jun 13, 2011 at 13:22 UTC

    Great! The output (more of the 2nd one) is what I was looking for. I guess I'll include it in the question next time.

    The HoA won't work for me, because in my actual code: in the AoA/AoH, the "inner" array/hash (the references) are 6 strings, not numbers. The "outer" array is the a list of all these "sets" of strings (which come from a search engine type of code and needs to be sorted).

    >(How do you indent?)>(which also means I'm going to create a more complex sorting method, where I sort the "outer" array by certain *values* of the hash, or elements by certain elements of the "inner" array).

    Should I include this type of background in my questions?

    Thanks a lot, I'm going to try and understand these codes.

    Thanks for explaining map{@$_}...$_ really screws me up, I take it that one comes from the elements of @all_matches

    .