Jump to content

StringSplit Update


Recommended Posts

I have been updating some of my older scripts for distrobution to some of my AutoIt supporting clients and came across something strange. I think when the StringSplit function was updated (before the 12/01 release) a new bug was introduced. According to the documentation:

If you use a blank string "" for the delimiters, each character will be returned as an element.

Well a month or so ago I posted my Random String Generation code on Scripts and Scraps and it worked fine, but now running the 12/01/04 release of 3.0.103 it won't seperate my string into an array by charaters. I have made some code changes and I am pretty sure it is clean code, but if another developer could take a look to make sure I am not missing something; I would appriciate it. The code is as follows:

Func GetRandString( $Len, $Options, $OtherChars = "" )
   
   Dim $Chars, $CharArray, $GenString
   
   If Not StringIsInt( $Len ) Then Return -1
   
   Select
      Case BitAND( $Options, 1 ); Include UpperCase
         $Chars = $Chars & "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      Case BitAND( $Options, 2 ); Include LowerCase
         $Chars = $Chars & "abcdefghijklmnopqrstuvwxyz"
      Case BitAND( $Options, 4 ); Include Numbers
         $Chars = $Chars & "0123456789"
      Case BitAND( $Options, 8 ); Include Special
         $Chars = $Chars & "~`!@#$%^&*()_+=-|}{[]\:';<>?/.," & Chr(34)
      Case BitAND( $Options, 16 ); Include Others
         $Chars = $Chars & $OtherChars
   EndSelect
   
   If $Chars = "" Then Return -2
   
   $CharArray = StringSplit( $Chars, "", 0 )
   
   For $i = 1 to $Len
      $GenString = $GenString & $CharArray[Int(Random( 1, $CharArray[0] ))]
   Next
   
   Return $GenString
   
EndFunc

Thanks guys!

Matt

EDIT: Removed Debug code

Edited by Matt @ MPCS
Link to comment
Share on other sites

Problem fixed! Below you will find my corrected code for those who are curious.

Func GetRandString( $Len, $Options, $OtherChars = "" )
   
   Dim $Chars, $CharArray, $GenString
   
   If Not StringIsInt( $Len ) Then Return -1
   
   If BitAND( $Options, 1 ) Then; Include UpperCase
      $Chars = $Chars & "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   EndIf
   If BitAND( $Options, 2 ) Then; Include LowerCase
      $Chars = $Chars & "abcdefghijklmnopqrstuvwxyz"
   EndIf
   If BitAND( $Options, 4 ) Then; Include Numbers
      $Chars = $Chars & "0123456789"
   EndIf
   If BitAND( $Options, 8 ) Then; Include Special Characters
      $Chars = $Chars & "~`!@#$%^&*()_+=-|}{[]\:';<>?/.," & Chr(34)
   EndIf
   If BitAND( $Options, 4 ) Then; Include User Defined
      $Chars = $Chars & $OtherChars
   EndIf
   
   If $Chars = "" Then Return -2
   
   $CharArray = StringSplit( $Chars, "", 0 )
   
   For $i = 1 to $Len
      $GenString = $GenString & $CharArray[Int(Random( 1, $CharArray[0] ))]
   Next
   
   Return $GenString
   
EndFunc

Matt

Link to comment
Share on other sites

Small bug fixed. This is part of a String/Filename library I am working on for my clients, so as soon as I get it done I plan to post the whole library (Under scripts and scraps where it belongs). Thanks to those of you using my code.

Func GetRandString( $Len, $Options, $OtherChars = "" )
   
   Dim $Chars, $CharArray, $GenString
   
   If Not StringIsInt( $Len ) Then Return -1
   
   If BitAND( $Options, 1 ) Then; Include UpperCase
      $Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   EndIf
   If BitAND( $Options, 2 ) Then; Include LowerCase
      $Chars = $Chars & "abcdefghijklmnopqrstuvwxyz"
   EndIf
   If BitAND( $Options, 4 ) Then; Include Numbers
      $Chars = $Chars & "0123456789"
   EndIf
   If BitAND( $Options, 8 ) Then; Include Special Characters
      $Chars = $Chars & "~`!@#$%^&*()_+=-|}{[]\:';<>?/.," & Chr(34)
   EndIf
   If BitAND( $Options, 16 ) Then; Include User Defined
      $Chars = $Chars & $OtherChars
   EndIf
   
   If $Chars = "" Then Return -2
   
   $CharArray = StringSplit( $Chars, "", 0 )
   
   For $i = 1 to $Len
      $GenString = $GenString & $CharArray[Int(Random( 1, $CharArray[0] ))]
   Next
   
   Return $GenString
   
EndFunc

Matt

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...