Yes, I have already found a way to get mode for the set of numbers in a couple of ways.

No need to sort the list before you populate the hash.

(one of my problemS) is getting a counter and a compare for highest value.

Well, yes, you'll still have to count the values per key, so you'll still need a hash in any reasonable solution. The clever stuff with a reference to the array only gets your data into your subroutine. The next steps: to sort it in order to find out the mode number, and to return the mode, are going to be the same however you get the data into the sub.

But you still aren't using strict and warnings and using a dumper to help you develop your code, as roboticus suggested in one of the most generous posts I can remember. If you were, you would get as far as:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @data = ( qw/ 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 +1 4 5 3 3 3 /); my $mode = mode( { NUMBERS => \@data } ); sub mode { my %opt = %{ shift @_ }; print Dumper \%opt; } __END__

. . . and you would see the contents of %opt:

$VAR1 = { 'NUMBERS' => [ '2', '3', '3', '3', '5', '7', '8', '12', '32', '44', '55', '12', '3', '23', '43', '33', '1', '4', '25', '43', '42', '1', '4', '5', '3', '3', '3' ] };

It could be that viewing the data like that doesn't help you see what's going on (because of the complicated way of passing the argument to the sub) ... so you can try a simpler way:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @data = ( qw/ 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 +1 4 5 3 3 3 /); my $mode = mode( { NUMBERS => \@data } ); sub mode { my %opt = %{ shift @_ }; while (my ($key, $val) = each %opt) { print "k:$key v:$val\n"; } } __END__

This outputs:

k:NUMBERS v:ARRAY(0x7fef9a82a168)

So now you can see that your %opts hash has only one key=value pair, and your list of numbers is still encapsulated in a reference in the single value in the hash. Probably not what you want, since your next line,

my @i_numbers = $opt{NUMBERS};

is expecting the value of the hash element to be an array. It's not. It's a reference to an array. So in order to use it, you'd either have to dereference it as you extract it from the hash, or capture into a reference:

my $i_numbers = $opt{NUMBERS}; # $i_numbers is an arrayref # or my @i_numbers = @{ $opt{NUMBERS} }; # dereference the arrayref as you +get it from the hash

. . . but either way, you are right back where you would have been if you had just passed a simple array to the sub: you still have to build a lookup table, i.e. a hash, in order to be able to sort by how many occurrences of each number there are. Here's the complete program: you can see that it has a level of complexity that is not needed, because of referencing the array at the beginning:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @data = ( qw/ 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 +1 4 5 3 3 3 /); my $mode = mode( { 'NUMBERS' => \@data } ); # I quote hash keys so I d +on't # mistake them for constan +ts # and use them as bareword +s. print "Mode : $mode\n"; sub mode { my %count = (); my %opt = %{ shift @_ }; for ( @{ $opt{'NUMBERS'} } ) { # no need to sort the array here $count{ $_ }++; } my @key = sort{ $count{$b} <=> $count{$a} } keys %count; return $key[0]; } __END__

I hope this reply showed you four things:

The way forward always starts with a minimal test.

In reply to Re^3: Help fixing this piece of code by 1nickt
in thread Help fixing this piece of code by perlynewby

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.