Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: unxpected sort warnings while using sort

by choroba (Cardinal)
on Jul 30, 2015 at 11:45 UTC ( [id://1136840]=note: print w/replies, xml ) Need Help??


in reply to Re^2: unxpected sort warnings while using sort
in thread unxpected sort warnings while using sort

#!/usr/bin/perl use warnings; use strict; sub most_duplicated { my @numbers = @_; my %count; ++$count{$_} for @numbers; my @most_duplicated; my $max_count = 0; for my $n (keys %count) { if ($count{$n} > $max_count) { @most_duplicated = ($n); $max_count = $count{$n} } elsif ($count{$n} == $max_count) { push @most_duplicated, $n; } } return ($max_count, \@most_duplicated) } 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 ($count, $most_duplicated) = most_duplicated(@data); print "@$most_duplicated occurred $count times.\n";

It's also possible to avoid two loops and do all the work in one loop, but I think this way is more understandable.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^4: unxpected sort warnings while using sort
by perlynewby (Scribe) on Jul 30, 2015 at 20:49 UTC

    thank you this is great solution.

    can you replicate the counter and method to getting most duplicated using this function method

    sub mode_num{ my %count=(); %opt = %{shift @_ } ; #creating hash #dump \%opt; #check data coming in.. #need to get integrate count as I format data into hash(next task + to make it reable) for (sort{$a <=> $b} %opt{NUMBERS}){ #dump \$opt{NUMBERS}++; $opt{NUMBERS}++; } our @keys= sort{$opt{NUMBERS}->{$b}<=>$opt{NUMBERS}->{$a}} key +s %opt{NUMBERS}; return }

    now, I would like to use this method above so I can see how the loop/counter/finding most duplicated with a hash_ref works ($opt->{NUMBERS}[])

    It intrigued me to follow this method. Unfortunately, I haven't gotten a solution yet

      i need help figuring out how to work this sub. thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found