in reply to unxpected sort warnings while using sort

Take out:
local $/; # slurp mode
since you want individual numbers in separate array elements.

It is also a good idea to

chomp @data;
after reading it in.

<=> is the right operator for sorting numerics.

instead of "(my @num= shift)", use

my @sorted=sort {$a<=>$b} @_;

        "Despite my privileged upbringing, I'm actually quite well-balanced. I have a chip on both shoulders."         - John Nash

Replies are listed 'Best First'.
Re^2: unxpected sort warnings while using sort
by perlynewby (Scribe) on Jul 22, 2015 at 23:40 UTC

    made changes but no change in output

    commented out slurp

    made reable code by using @_ as used per subs

    no sort and no change in message

    open my $in, '<', 'numbers.txt' or die $!; # local $/; # slurp mode my @data = <$in>; chomp @data; close $in; sort_num(@data); my @new_sort=sort_num(); print "this is new sorted data ;",@new_sort; sub sort_num{ my @sorted=sort {$a <=> $b}@_; return @sorted; }
    Argument "2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1" isn' +t numeric in sort at C:\Users\Alberto\Documents\NetBeansProjects\Perl +Project\Perl Essentials\subroutines\ave_mode_median_range sub.pl line + 24. this is new sorted data ;

      What is your data? The warning message
          Argument "2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1" isn't numeric in sort at ...
      shows that Perl still thinks it's a single string with a bunch of digits and spaces in it. sort will never sort this numerically — or any other way, since it's just a single-element list and so is already "sorted"!


      Give a man a fish:  <%-(-(-(-<

      As AnomalousMonk has pointed out, the way your data is formatted is important.

      The way most programmers would expect to see your data is like this:

      2 3 3 3 5 7 8 12 ...
      If, instead, it looks like this:
      2 3 3 3 5 7 8 12 ...
      then you have a problem .. or rather, it needs to be handled differently in your code.

      For the second case, you need:

      my @data = split /\s+/,<$in>;

      In addition, the way the "sort_num" is called has some issues in your code.

      This is how it should be called:

      my @new_sort = sort_num (@data);
      And call it only ONCE.

              "Despite my privileged upbringing, I'm actually quite well-balanced. I have a chip on both shoulders."         - John Nash

        correct formatting of data is something new to me...I never thought about it once in this case of numbers vs strings but ,now, I will.

        One more question

        can anyone explain why I can't do slurp while populating the data var? I am aware of the memory issue that may arise if file is big but for these little programs is not an issue to me...but then again I am learning and you may have some experiences to share ;-)

         local $/;    # slurp mode

        thank you all!

        learning to do subs and modules now so many more new things to think about

      Strongly suggest using Data::Dumper to see what your data is and looks like.