Well, after more testing, I found that my first version (labelled "frontal 
attack") doesn't always work.  Just in case anyone other than me cares, here's 
a new version that seems to work properly.  Sorry for the noise.
Boyd


    // frontal attack
        //      testString = @"0756013";
                testString = @"4756013";
        //      testString = @"0756";
        //      testString = @"0";
    
    // alleles and digitsPerAllele are passed as arguments; the values shown 
here are just for testing
    int alleles = 3;
    NSUInteger digitsPerAllele = 3;

    NSString *subString;
    NSRange range;
    int theInt;
    int i;
    int sLength = [testString length];
    range.location = 0;
    int remainder = [testString length] % digitsPerAllele;
    if (remainder > 0) {
        range.length = remainder;
        subString = [testString substringWithRange:range];
        theInt = [subString intValue];
        --alleles;
        range.location = remainder;
        sLength = sLength - remainder;
    }
    if (sLength > 0) {
        range.length = digitsPerAllele;
        for (i = 0; i < alleles; i++ ) {
            subString = [testString substringWithRange:range];
            theInt = [subString intValue];
            range.location += digitsPerAllele;
        }
    }


Begin forwarded message:

> From: Boyd Collier <bcolli...@cox.net>
> Subject: Re: reverse scanner
> Date: August 12, 2013 1:00:56 PM PDT
> To: Cocoa Dev List <cocoa-dev@lists.apple.com>
> 
> I've come up with a couple of reasonably straight-forward solutions to my 
> problem, but first, thanks to everyone who offered suggestions. Even though I 
> didn't choose to use them, your willingness to make them is definitely 
> appreciated.
> 
> Here are my solutions (note that there are 2, one labelled "attack from the 
> rear", the other labelled "frontal attack").  I won't claim that they have 
> been really thoroughly tested, but I have tried a variety of values for 
> testString, d, digitsPerAllele and alleles and both methods appear to work.  
> If anyone sees a problem or an improvements, please let me know.  Right now, 
> I favor the frontal attack, which occurred to me in the middle of the night 
> -- I seldom have reason to use the % operator and so didn't think of it right 
> away.  Of course, in actual use, I'll first take steps to separate the string 
> that's being processed into substrings, if the original string uses a ";" 
> (for example) to separate allelic values, e.g. if the string being processed 
> is 04;756;013.
> 
>     NSString *testString = @"0756013";
>     NSUInteger d = 3;
>     NSUInteger digitsPerAllele = d;
>     NSUInteger alleles = 3;
>     
>     NSString *subString;
>     NSRange range;
>     int theInt;
>     
>     // attack from the rear
>     int startOfRange = [testString length];
>     int length = d;
>     int i;
>     for (i = 0; i < alleles; i++ ) {
>         startOfRange = startOfRange-d;
>         if (startOfRange < 0) {
>               length = length + startOfRange; // note that startOfRange is 
> negative here
>               if (length <= 0)
>                 break;
>             startOfRange = 0;
>             d = length;
>               }
>         range.location = startOfRange;
>       range.length = d;
>         subString = [testString substringWithRange:range];
>         theInt = [subString intValue];
>     }
> 
>     // frontal attack
>     testString = @"04756013";
>     int remainder = [testString length] % digitsPerAllele;
>     range.location = 0;
>     range.length = remainder;
>     subString = [testString substringWithRange:range];
>     theInt = [subString intValue];
>     --alleles;
>     range.location = remainder;
>     range.length = digitsPerAllele;
>     for (i = 0; i < alleles; i++ ) {
>       subString = [testString substringWithRange:range];
>         theInt = [subString intValue];
>         range.location += digitsPerAllele;
>     }
> 
> 
> On Aug 10, 2013, at 10:07 AM, Boyd Collier <bcolli...@cox.net> wrote:
> 
>> I'm dealing with a situation in which I have to scan strings that are 
>> separated by tabs, and for each string, I have to extract two numerical 
>> values, with these values being separated by a non-numerical character or 
>> not being separated by any character at all.  I know the maximum number of 
>> characters used to represent each number, but unfortunately, the first 
>> character in the group of characters used to represent the first number can, 
>> quite arbitrarily, be either a 0 or missing.  For example, with the number 
>> of characters used to represent each number known to be 2, the strings 607, 
>> 0607, 06;07 (note the semicolon between 06 and 07) should all result in 6 
>> and 7 being extracted as the two numerical values. Of course, I'd like to do 
>> something simple, and were it not for the arbitrary inclusion of a leading 
>> 0, it would be quite simple to use an instance of NSScanner.  Or, if there 
>> were such a beast as NSReverseScanner, it would also be relatively straight 
>> forward, but so far as I'm aware, no such beast exists. I can think of a 
>> couple of ways do accomplish this task, but if someone has already come up 
>> with a clean way of scanning in reverse, I'd appreciate hearing from them.
>> 
>> Boyd
> 

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to