I was fiddling today with something that involved comparing strings and I thought that "Isn't a shame that the inequality operators don't return the point of divergance." I mean I think it would be nice to be able to say:
if (my $pos=$str1 ne $str2) { print "A:$str1\nB:$str2\n"; print "A and B are different at the $pos'th char (index ".($pos-1)." +)\n"; }
Likewise for the other inequality operators.
Also castaway pointed out that if '0E0' is used to represent the divergance at index 0 then the overal return need not have the additional +1. OTOH it would mean the code would look like:
if (my $pos=$str1 gt $str2) { $pos+=0; print "A:$str1\nB:$str2\n"; print "The ".(1+$pos)."'th (index $pos) char in A is 'greater' than +that in B\n"; }
I'm not sure which exceptional behaviour I like more (adding one to the offset or using '0E0' as the return for index 0). But either way would more useful in many situations than the current True/False.
I wonder if this were to be added would it slow Perl down much? I would have thought that most of the extra stuff could be optimised away when the return won't be used... What do you think?
Minor Update: Presumably this behaviour could also extend to the numerical comparison operators by having it return the lower or greater value. So while != wouldn't be covered but the > < >= <= operators could return the relevent value so my $is_smaller=$x < 7; would set $is_smaller to $x (or 0E0 if it was 0) for all $x < 7; and it would set it to undef for all $x>=7. In which case obviously castaway's "0E0 for a 0" idea makes more sense than the offset+1, as it works in both the numerical inequality operators as well as the numerical. Although I have a feeling that something here would break perl as it currently works while I don't think thats true of the string comparisons.
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Should string inequality operators return the point the of divergance?
by Corion (Patriarch) on Oct 15, 2004 at 09:34 UTC | |
by tilly (Archbishop) on Oct 15, 2004 at 15:20 UTC | |
|
Re: Should string inequality operators return the point the of divergance?
by TedYoung (Deacon) on Oct 15, 2004 at 12:49 UTC | |
|
Re: Should string inequality operators return the point the of divergance?
by hv (Prior) on Oct 15, 2004 at 12:47 UTC | |
by Corion (Patriarch) on Oct 15, 2004 at 12:58 UTC | |
|
Re: Should string inequality operators return the point the of divergance?
by castaway (Parson) on Oct 15, 2004 at 09:19 UTC | |
|
Re: Should string inequality operators return the point the of divergance?
by ambrus (Abbot) on Oct 15, 2004 at 10:11 UTC |