in reply to Sort Says "not numeric" then sorts numeric anyway?
And strict complains, because the arguments aren't numeric.Actually, it's warnings that is complaining.
Transforming the comparison elements with int($a) won't keep warnings silent either. Turning off warnings temporarily gets rid of the messages, but I wouldn't recommend it. Warnings are useful because they tell you that something is wrong and needs your attention.#!/usr/bin/perl -w use strict; my @x = qw( 1xxx 2xxx 300x 10xx ); @x = sort @x; print "default sort: @x\n"; @x = sort {local $^W; $a <=> $b} @x; print "numeric sort: @x\n";
And your array is not sorted at all.@x = qw( 5xxx 2xxx 300x 10xx ); @x = sort {$a =~/^d+/ <=> $b =~/^d+/} @x; # WRONG print "regex sort: @x\n";
@x = qw( 5xxx 2xxx 300x 10xx ); @x = sort {my ($y) = $a =~/^(\d+)/; my ($z) = $b =~/^(\d+)/; $y <=> $z} @x; print "regex sort: @x\n";
_ _ _ _ (_|| | |(_|>< _|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sort Says "not numeric" then sorts numeric anyway?
by Hofmator (Curate) on Feb 11, 2003 at 08:56 UTC | |
|
Re: Sort Says "not numeric" then sorts numeric anyway?
by Abigail-II (Bishop) on Feb 11, 2003 at 10:08 UTC |