hello monks
Ok, I fixed most of my problems
but I found another instance where my @sorted= shift@_ didn't work. it worked with @_ only
my @sorted= @_; #using shift didn't work, why? format?
trying to read about it to find solution/explanation but no luck yet
Anyhow, still looking to create an algorithms to find the element that appears the most and return it
not too much luck so far but working on this still
Monks , I am updating the previous post coz yesterday I was to frustrated not finding solutions to my issues
still trying to find how to properly use/pass a var=value from inside one sub into another sub
this is the code I fix
use strict; use warnings; use autodie; use Data::Dump qw(dump); open my $in, '<', 'numbers.txt' or die $!; my @data =split/\s+/,<$in>; close $in; my ($mean,$ref,@new_sort)= sort_num(@data); my $length=$$ref; #passing by ref example #print "sorted : @new_sort\n"; print "mean : $mean\n"; print "length : $length\n"; my ($middle,$median)=median_num(@new_sort); print "this is median : $median\n"; print "this will be middle num position : $middle\n"; my ($size,$hash_ref)=mode_num( {NUMBERS=>\@new_sort} ); # my $true_size=$$size; print "this is length :$true_size\n"; foreach my $k (keys %$hash_ref){ for (0..$length){ print " $hash_ref->{$k}[$_]"; #%hash->NUMBER=> i_number #there are + undef but dump doesn't show these } } # 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 %opt = %{shift @_ } ; #creating hash my $size=keys %opt->{NUMBERS}; #getting size of hash #my %count=(); #for (0..$size){ #have to defined the hash coz perl will see 0 if undef # foreach my $key ( keys %opt->{$key}){ # my $value= $opt->{$key}[$_]; # if (not exists $count{$value}){ # $count{$value}=[]; # } # push $count{$value},$key; # } #} return \$size,\%opt; }
In reply to unxpected sort warnings while using sort by perlynewby
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |