Jump to content
Sign in to follow this  
flyingboz

Incrementing Strings

Recommended Posts

flyingboz

On Several Occasions I've had to parse and manipulate strings as numbers - I finally generalized one such - might help somebody out or give someone some ideas for a Decimal Arithmetic include file. I use stuff like this a lot to

(a) generate unique filenames

(:whistle: locate or search for related filenames

© locate relevant strings in reports. web pages, and other text elements

;===============================================================================
#cs
; Description:    Given a string representation of a positive integer, add one to it.
; Syntax:          _IncrementStringAsNum($string)
; Parameter(s):  $string 
; Requirement(s):   $string must be composed solely of integers [0-9]
; Return Value(s):  On Success - Returns $result such that int($string) + 1 = int($result)
;                  On Failure : -1 if any element of $string is not an integer
;                                           -2 if StringLen($result) would be >  $stringlen($string)
; Author(s):        FlyingBoz (flyingboz@yahoo.com)
; Note(s):       
              ;This function was designed to be used as a portion of a filename - a unique incrementer,
                 ;requiring some decimal arithmetic.  The code ensures that the string is composed of valid
                 ;integers, and it is assumed that the length of the string is sufficient to handle usage.
                 ;should you desire to increment without bound, remove/ adjust the lines invoiving $max
                 ;per your requirments.  This abstracts the number of digits required, to help filesorting,
                 ;ensure filenamelength compliance, etc. as written it will not return a string of greater length
                 ;than passed.
;===============================================================================
#ce


Func _IncrementStringAsNum($a)
   
   Local $a; array containing the elements comprising the digits
   Local $max; the largest integer possible for StringLen($a[0])
   Local $result; incremented string
   $a = StringSplit($a, '')
   
   For $i = 1 To $a[0]
      $a[$i] = Int($a[$i])
      If @error Then Return -1
      
   Next
   $max = ''
   For $i = 1 To $a[0]
      $max = $max & 9
   Next
   If Int($max) = $int Then Return -2
  ;increment
   $a[$a[0]] = $a[$a[0]] + 1
  ;carry the one :)
   For $i = $a[0] To 1 Step - 1
      If $a[$i] = 10 Then
         $a[$i] = 0
         $a[$i - 1] = $a[$i - 1] + 1
      EndIf
   Next
   $result = ""
  ;build the result
   For $i = $a[0] To 1 Step - 1
      $result = $a[$i] & $result
   Next
   Return $result
EndFunc  ;==>_IncrementStringAsNum

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites
CephasOz

Going from your comments, I think the following code does the same job.

Func  _IncrementStringAsNum( $sNum )
    Local $iNum = Int( $sNum ) + 1
    If Not StringIsDigit( $sNum ) Then Return -1
    If StringLen( String( $iNum ) ) > StringLen( $sNum ) Then Return -2
    Return StringFormat( "%0" & StringLen( $sNum ) & "d", $iNum )
EndFunc

The code below doesn't work properly.

1) It re-declares a parameter ($a) as a local variable,

2) It doesn't pick up strings with non-digit characters (try "A4"), and

3) It uses a variable ($int) that doesn't get assigned a value.

Hope that's helpful.

On Several Occasions I've had to parse and manipulate strings as numbers - I finally generalized one such - might help somebody out or give someone some ideas for a Decimal Arithmetic include file.  I use stuff like this a lot to

(a) generate unique filenames

(:whistle: locate or search for related filenames

© locate relevant strings in reports. web pages, and other text elements

;===============================================================================
#cs
; Description:    Given a string representation of a positive integer, add one to it.
; Syntax:          _IncrementStringAsNum($string)
; Parameter(s):  $string 
; Requirement(s):   $string must be composed solely of integers [0-9]
; Return Value(s):  On Success - Returns $result such that int($string) + 1 = int($result)
;                  On Failure : -1 if any element of $string is not an integer
;                                           -2 if StringLen($result) would be >  $stringlen($string)
; Author(s):        FlyingBoz (flyingboz@yahoo.com)
; Note(s):       
             ;This function was designed to be used as a portion of a filename - a unique incrementer,
                ;requiring some decimal arithmetic.  The code ensures that the string is composed of valid
                ;integers, and it is assumed that the length of the string is sufficient to handle usage.
                ;should you desire to increment without bound, remove/ adjust the lines invoiving $max
                ;per your requirments.  This abstracts the number of digits required, to help filesorting,
                ;ensure filenamelength compliance, etc. as written it will not return a string of greater length
                ;than passed.
;===============================================================================
#ce
Func _IncrementStringAsNum($a)
   
   Local $a; array containing the elements comprising the digits
   Local $max; the largest integer possible for StringLen($a[0])
   Local $result; incremented string
   $a = StringSplit($a, '')
   
   For $i = 1 To $a[0]
      $a[$i] = Int($a[$i])
      If @error Then Return -1
      
   Next
   $max = ''
   For $i = 1 To $a[0]
      $max = $max & 9
   Next
   If Int($max) = $int Then Return -2
 ;increment
   $a[$a[0]] = $a[$a[0]] + 1
 ;carry the one :)
   For $i = $a[0] To 1 Step - 1
      If $a[$i] = 10 Then
         $a[$i] = 0
         $a[$i - 1] = $a[$i - 1] + 1
      EndIf
   Next
   $result = ""
 ;build the result
   For $i = $a[0] To 1 Step - 1
      $result = $a[$i] & $result
   Next
   Return $result
EndFunc ;==>_IncrementStringAsNum

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites
flyingboz

Going from your comments, I think the following code does the same job.

Func  _IncrementStringAsNum( $sNum )
    Local $iNum = Int( $sNum ) + 1
    If Not StringIsDigit( $sNum ) Then Return -1
    If StringLen( String( $iNum ) ) > StringLen( $sNum ) Then Return -2
    Return StringFormat( "%0" & StringLen( $sNum ) & "d", $iNum )
EndFunc

I hadn't noticed the StringFormat function nice - the logic errors slipped in when migrating from a previous implementation. Nice, and efficient. ty

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.