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

Monks

I need help with my sub routine

I cannot find a way to create a counter and test for the most duplicated number in the hash

have tried on my own but not successful. Please help so I can see how to do this properly

problem section below
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 $opt{NUMBERS}[0]; #some $keys[0] here but don't know proper +syntax...or correct method }
DATA
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
Complete code
# will find ave, media, range,mode and mean use strict; use warnings; use autodie; use Data::Dump qw(dump); open my $in, '<', 'numbers.txt' or die $!; # local $/; # slurp mode my @data =split/\s+/,<$in>; close $in; my ($mean,$ref,@new_sort)= sort_num(@data); my $length=$$ref; #passing by ref example print "Input Data set : @new_sort\n"; print "Mean : $mean\n"; print "Lenght : $length\n"; my ($middle,$median)=median_num(@data); print "Median : $median\n"; print "Position of median number : $middle\n"; #using reference to build hash and find most duplicated num my ($hash_ref)=mode_num( {NUMBERS=>\@new_sort} ); #foreach my $k (keys %$hash_ref){ # for (0..$length){ # print " $hash_ref->{$k}[$_]"; #%hash->NUMBER=> i_number # } # } my ($mode_num)=alt_mode_num(@data); print "Mode : $mode_num\n"; # my sub practice with ref and hash_ref sub sort_num{ our @sorted=sort {$a <=> $b}@_; our $length = scalar (@sorted); our $add_div_elem =(eval join '+',@sorted)/$length; median_num(\@sorted); return $add_div_elem,\$length,@sorted; } sub median_num{ my @sorted= @_; #using shift didn't work, why? format? my $middle_one = eval ((scalar @sorted)+ 1)/2; my $median=$sorted[$middle_one]; return ( $middle_one, $median) } #passing by references 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 } #alternate to get most repeated num. need to know while using NUMBE +RS ref sub alt_mode_num{ my %count=(); for (sort{$a <=> $b} @_){ $count{$_}++; } my @key= sort{$count{$b}<=>$count{$a}} keys %count; return $key[0]; }

Replies are listed 'Best First'.
Re^3: unxpected sort warnings while using sort
by choroba (Cardinal) on Jul 30, 2015 at 11:45 UTC
    #!/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.

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

      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