danl001 wrote:

Here, the first 10 characters correspond but then the second string runs out. Using our rule, we'd order ABC-MARKET before ABC-MARKET.ABC-MARKET, which is wrong. I guess I could try following that rule, but if the character position in the longer string that corresponds to the first position in the shorter string where there is no character is a "-" or a ".", then the longer string is "less". Just a thought. I'll have to check out the data more and try it out.


Oh no! Its slower! I wrote a function implementing what is described above and its actually slower (about 1/2 as slow) than that huge thing I posted earlier. Does anything stand out here as being inefficient? Here it is:

sub compare {
    my $a = shift;
    my $b = shift;

    my $asciiDASH = 45;
    my $asciiDOT = 46;

    my @a = unpack("C*", $a);
    my @b = unpack("C*", $b);

    foreach my $achar (@a) {
        my $bchar = shift(@b);

        if (defined($achar)) {
            if (defined($bchar)) {
                 # both defined
                 if ($achar == $bchar) {
                     #print "$achar == $bchar\n";
                     next;
                 } elsif ($achar > $bchar) {
                     #print "$achar > $bchar\n";
                     return 1;
                 } else  {
                     #print "$achar < $bchar\n";
                     return -1;
                 }
            } else {
                # achar defined, bchar not
                if ($achar == $asciiDASH) {
                    #print "achar: $achar is dash\n";
                    return -1;
                } else {
                    #print "achar: $achar is NOT dash\n";
                    return 1;
                }
            }
        } else {
            # achar undefined
            if (defined($bchar)) {
                # bchar defined, achar not
                if ($bchar == $asciiDASH) {
                    #print "bchar: $bchar is dash\n";
                    return 1;
                } else {
                    #print "bchar: $bchar is NOT dash\n";
                    return -1;
                }
            } else {
                # both undefined
                return 0;
            }
        }
    }

    my $bchar = shift(@b);
    if (!defined($bchar)) {
        return 0;
    } else {
        if($bchar == $asciiDASH) {
           #print "bchar: $bchar is dash\n";
           return 1;
        } else {
           #print "bchar: $bchar is NOT dash\n";
           return -1;
        }
    }

}




-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to