in reply to unxpected sort warnings while using sort
You really need to start learning to use the debugging tools you've chosen -- in thise case, Data::Dump.
I wrote my own data dumper so I apologize for using a nonstandard module here to demonstrate, but it gets the point across quite nicely. I made the following changes:
sub median_num{ my @sorted= @_; #using shift didn't work, why? format? &debug::debugdumplist("median_num(): \@sorted", \@sorted);
Notice in the results how median_num()receives two different types of structures:
D:\PerlMonks>shift3.pl Using a hash as a reference is deprecated at D:\PerlMonks\shift3.pl li +ne 54. median_num(): @sorted (S:/Steve/Perl/debug.pm:887): [ARRAY(0x2ae0c28)] [1] [1] [2] [3] [3] [3] [3] [4] [5] [7] [8] [12] [12] [23] [25] [32] [33] [42] [43] [43] [44] [55] mean : 18.3636363636364 length : 22 median_num(): @sorted (S:/Steve/Perl/debug.pm:887): [1] [1] [2] [3] [3] [3] [3] [4] [5] [7] [8] [12] [12] [23] [25] [32] [33] [42] [43] [43] [44] [55] this is median : 12 this will be middle num position : 11.5 this is length :22 Use of uninitialized value in concatenation (.) or string at D:\PerlMo +nks\shift3.pl line 29. 1 1 2 3 3 3 3 4 5 7 8 12 12 23 25 32 33 42 43 43 44 55 D:\PerlMonks>
Look closely at the first one; the @sortedarray contains another ARRAY; and that array then has scalar values in it. This is an array of arrays.
Look at the second one; no embedded ARRAY; @sortedsimply contains scalar values.
So I looked at the lines in your code which call median_num(), and lo and behold, you are not using the subroutine consistently:
my ($middle,$median)=median_num(@new_sort); median_num(\@sorted);
The first line calls median_num()with an array as its parameter list.
The second one calls it with a REFERENCE to an array. In Cterms, you've passed it by reference instead of by value
Betting this inconsistency has something to do with why shiftdidn't work.
Also, you might want to clean up your errors and warnings, but you probably knew that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: unxpected sort warnings while using sort
by perlynewby (Scribe) on Jul 28, 2015 at 20:47 UTC | |
|
Re^2: unxpected sort warnings while using sort
by perlynewby (Scribe) on Jul 30, 2015 at 00:15 UTC | |
by choroba (Cardinal) on Jul 30, 2015 at 11:45 UTC | |
by perlynewby (Scribe) on Jul 30, 2015 at 20:49 UTC | |
by perlynewby (Scribe) on Jul 31, 2015 at 23:17 UTC |