gives the error:############################### # try to sort fields as numbers first, then as strings # keep going until there's a difference sub smart_sort { my @a = split /[\s_]+/, lc($a); my @b = split /[\s_]+/, lc($b); for my $i ( 0..(@a < @b ? $#a : $#b) ) { if ( $a[$i] =~ $NUMBER_ONLY_REGEX # line 214 and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } elsif ( ( my @ai = /^(\d+)(.+)/ ) # error/bug is here and ( my @bi = /^(\d+)(.+)/ ) ) { if ( $ai[0] <=> $bi[0] ) { return $ai[0] <=> $bi[0]; } elsif ( $ai[1] cmp $bi[1] ) { return $ai[1] cmp $bi[1]; } } elsif( $a[$i] cmp $b[$i] ) { return $a[$i] cmp $b[$i]; } } return $a cmp $b; }
Use of uninitialized value in pattern match (m//) at /user/sgriffit/perl/mergen.pl line 214.Line 214 is only the start of the if/elsif/... chain. Not too helpful. Glad there's only 1 place where a match is happening.
Now I realize there are several things there that could be changed to improve the error reporting. For instance, since most of the blocks return, the elsifs could be ifs instead.
I don't really want to spend time thinking "If this code has a bug, what's the best way of writing it so it's easy to spot?" Perhaps I'm flogging a dead horse...
Here's the cleaned up version, with more comments:
sub smart_sort { my @a = lc($a) =~ /(\d+|[a-z]+)/ig; # better split on fields my @b = lc($b) =~ /(\d+|[a-z]+)/ig; for my $i ( 0..(@a < @b ? $#a : $#b) ) { # try numbers first if ( $a[$i] =~ $NUMBER_ONLY_REGEX and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } # fallback to string compare if( $a[$i] cmp $b[$i] ) # no "elsif" { return $a[$i] cmp $b[$i]; } } # can't decide by fields, decide by whole string return $a cmp $b; }
-QM
--
Quantum Mechanics: The dreams stuff is made of
In reply to No Pause on Elsif in Debugger, Part 2 by QM
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |