Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: testing equality for the last digit in a string

  1. #11
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Thanks for the reply, the problem is not finding the last digit, but limiting it to the actuall last digit, so my if statements are currently picking up anything the same as the last digit, even if they aren't.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm still barely following.

    But I think you can solve this by just only looking at the two substrings-- split the last digit from everything else and then use just those two to compare.

    (Also, it looks like you could just limit your for loop by one and never get to the last digit in the loop, but that's just another method.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #13
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    I made this modification:
    Code:
    $oddNums = array(); 
    $evenNums = array();
    $barcode = '123456789999'; 
    $check = str_split($barcode, (strlen($barcode) - 1)); 
    
    for($i = 0; $i <= strlen($barcode); $i++){ 
    	$focus = $barcode[$i]; // 
    	
    	if(($i == 0 || ($i % 2) == 0) && $focus != $check[1]){ 
    		array_push($oddNums, $focus); 
    	}
    	if(($i == 1 || (($i % 2) != 0)) && $focus != $check[1]){ 
    		array_push($evenNums, $focus);
    	}
    	
    }
    however the problem is still there, so instead of the oddNums array being:

    1, 3, 5, 7, 9, 9,

    it's:

    1, 3, 5, 7,

    as the if statements are ignoring the nine's as they are the same as the final digit, so it thinks that they are the check digit and therefore ignore them.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  4. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm really not following what's wrong with that (it's not due to it being identical with the last digit-- try changing them).

    Why do you have this part? $focus != $check[1]? I think that's what is getting in the way. Is it supposed to be there?

    Here's your code rewritten:
    PHP Code:
    $barcode '123456789999'
    $check substr($barcode,0,-1);
    $last substr($barcode,-1);

    for(
    $i 0$i <= strlen($check); $i++){ 
        if(
    $i%2==0){ 
            
    $oddNums[] = $barcode[$i]; 
        }
        else { 
            
    $evenNums[] = $barcode[$i]; 
        }
    }

    //use as you wish:
    echo $last//last digit
    echo $check//all but last digit
    print_r($evenNums); //all of the even numbers
    print_r($oddNums); //all of the odd numbers 
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #15
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    I was assuming there might be other characters in the string. I did say:

    Unless it's also always the last character in the string . . .
    People don't always read what you type. At that point bernie1227 should have replied that it is always the last character.
    I suppose I could have asked it as a direct question though.

    Anyways, thinking that it might not be, I came up with:

    PHP Code:
    <?php
    $pattern 
    '/.*(\d)[^\d]*$/'
    $string '12394do9cas7b21oacpp';
    $lastdigit ''
    preg_match($pattern$string$array);
    print_r($array); //diagnostic 
    if(isset($array) and isset($array[1])){ 
        
    $lastdigit $array[1]; 
        
    $pos strrpos($string$lastdigit); 
        echo 
    '<br>' $lastdigit ' is the last digit in' $string ', ' $pos ' characters from it\'s beginning<br>'
        echo 
    'Here\'s the string without it: ' substr($string0$pos) . substr($string$pos +1); 
    } else {
        echo 
    '<br>Either there is no string, or it contains no digits'
    }
    ?>
    The above will also work with all numbers.

    If it is always the last character, Daniel's method is more or less what I had in mind, go with it.
    Last edited by jscheuer1; 08-26-2012 at 02:26 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #16
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    So, you figure this one out yet? In any case, try this:

    PHP Code:
    <?php
    $string 
    '16598236';
    echo 
    'Starting String: ' $string '<br>'
    $lastindex strlen($string) - 1
    $lastdigit $string{$lastindex}; 
    echo 
    'Last Char: ' $lastdigit '<br>'
    $shortstring substr($string0$lastindex); 
    echo 
    'Shortened String: ' $shortstring '<br>';
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #17
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    thanks for the replies John, Haven't figured it out yet, I'm pretty sure I sad numerous times that I was after the last digit . I don't have time at the moment, but could you be so kind as to add this check and test it?
    PHP Code:
    <?php 
    $string 
    '16598236'
    echo 
    'Starting String: ' $string '<br>';  
    $lastindex strlen($string) - 1;  
    $lastdigit $string{$lastindex};  
    echo 
    'Last Char: ' $lastdigit '<br>';  
    $shortstring substr($string0$lastindex);  
    echo 
    'Shortened String: ' $shortstring '<br>'

    if(
    $lastdigit == 6){
        echo 
    "see what I mean?";
    }
    else{
        echo 
    "wow, it works!";
    }
    ?>
    you'll see what I've been trying to say. My main problem is that for the example here, my current code thinks that anything the same as the last digit, ie. 6, must be the last digit.
    Do you see what I'm trying to say?
    thanks for your continued efforts,
    Bernie
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  8. #18
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    6 will always == 6, there's no getting around that. But I think you've "painted yourself into a corner" if you think you have to make it otherwise. Clearly your objective cannot be to prove that a number isn't equal to itself. What're you looking for? If you wish to process each number in the string except the last number, use (from this most recent code) the $shortstring variable. It contains all the numbers in their original order except the last one.

    If you have some other objective, what might that be?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #19
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    ahhhhhh,
    I see what you're saying now, thankyou very much John
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  10. #20
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Bernie, I'm still completely lost. Read what you wrote earlier and see if it makes sense to you:
    I'm pretty sure I sad numerous times that I was after the last digit
    We've given you code to find the last digit.

    Are you intending some other use of "last"? I'm really confused.... not trying to be difficult.

    Or have you now solved it?

    If not, maybe you misunderstood the algorithm for a barcode. I don't know what it is, but what you've said hasn't been entirely clear either.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •