money Posted November 22, 2011 Share Posted November 22, 2011 (edited) Some older functions that were collecting dust._StringShuffle - Shuffles characters of a string_StringUnique - Return unique/non-dupe characters from a string_StringSort - Sort characters of a string_ArrayShuffle - Takes the given array and shuffles the order in which the elements appear in the array.Functions:expandcollapse popupGlobal $sString ; _StringShuffle Example $sString = _StringShuffle("The barking fat and yellow dog wore two collars for good luck") MsgBox(0, '', $sString) ; _StringUnique Example $sString = "abb2222bbbcccccc1112222223333" MsgBox(0, 'before',$sString) $sString = _StringUnique($sString) MsgBox(0, 'after',$sString) ; _StringSort Example #include <Array.au3> $sString = "ybhg73vdhvygczi87%$86bgtdfHHjNJUI*d_" MsgBox(0, 'before', $sString) $sString = _StringSort($sString, 0, 0, 0) MsgBox(0, 'after', $sString) ; Shuffles characters of a string ; by money Func _StringShuffle($sString, $sDelim = "") Local $sOutString, $iA, $cTmp ;Convert string to an array of characters for easier processing Local $aCharArray = StringSplit($sString, $sDelim, 1) ;Go through entire string length For $i = 1 To $aCharArray[0] ;Pick A random placement $iA = Random(1, $aCharArray[0], 1) ;Do nothing for same value placements If $i = $iA Then ContinueLoop ;random fails at this point, get value of last element If $iA = 0 Then $iA = $aCharArray[0] ;Store copy of 1st element value $cTmp = $aCharArray[$i] ;Overwrite 1st element value with 2nd element value $aCharArray[$i] = $aCharArray[$iA] ;Overwrite 2nd element value with 1st element value $aCharArray[$iA] = $cTmp Next ;Rebuild the string For $i = 1 To $aCharArray[0] $sOutString &= $aCharArray[$i] & $sDelim Next Return $sOutString EndFunc ;==>_StringShuffle ; Return unique/non-dupe characters from a string ; by money Func _StringUnique($sString) Local $sChr, $iASCII, $iStrLoc = 0, $iStrLen = StringLen($sString), $sOutString = "" While $iStrLoc < $iStrLen $iStrLoc += 1 ;Get the next character $sChr = StringMid($sString, $iStrLoc, 1) ;Get the ASCII code of the current character $iASCII = AscW($sChr) ;Check if the assigned ASCII code already exists If IsDeclared($iASCII) <> -1 Then ;Assign the ASCII code as a local variable Assign($iASCII, '', 1) $sOutString &= $sChr EndIf WEnd Return $sOutString EndFunc ;==>_StringUnique ; Sort characters of a string ; requires #include <Array.au3> ; by money Func _StringSort($sString, $iDesc = 0, $iStart = 0, $iEnd = 0) Local $aCharArray, $sOutString = "" Local $aCharArray = StringSplit($sString, "", 2) Local $iUbound = UBound($aCharArray)-1 If $iEnd = 0 Then $iEnd = $iUbound __ArrayQuickSort1D($aCharArray, $iStart, $iEnd) If $iDesc = 1 Then For $i = $iUbound To 0 Step -1 $sOutString &= $aCharArray[$i] Next Else For $i = 0 To $iUbound Step 1 $sOutString &= $aCharArray[$i] Next EndIf Return $sOutString EndFunc ;==>_StringSortBonus: _ArrayShuffleexpandcollapse popup; Begin Example ===================== #include <Array.au3> Local $avArray[10] $avArray[0] = "JPM" $avArray[1] = "Holger" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Jeremy" $avArray[5] = "Valik" $avArray[6] = "Cyberslug" $avArray[7] = "Nutster" $avArray[8] = "JdeB" $avArray[9] = "Tylo" _ArrayDisplay($avArray, "$avArray BEFORE _ArrayShuffle()") _ArrayShuffle($avArray) _ArrayDisplay($avArray, "$avArray AFTER _ArrayShuffle()") ; End Example ===================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _ArrayShuffle ; Description ...: Takes the given array and shuffles the order in which the elements appear in the array. ; Syntax.........: _ArrayShuffle(ByRef $avArray[, $iStart = 0[, $iEnd = 0]]) ; Parameters ....: $avArray - Array to shuffle ; $iStart - [optional] Index of array to start modifying at ; $iEnd - [optional] Index of array to stop modifying at ; Return values .: Success - 1 ; Failure - 0, sets @error to: ; |1 - $avArray is not an array ; |2 - $iStart is greater than $iEnd ; |3 - $avArray is not a 3 dimensional array ; Author ........: money ; Modified.......: ; Remarks .......: ; Related .......: _ArrayReverse, _ArraySort ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _ArrayShuffle(ByRef $avArray, $iStart = 0, $iEnd = 0) If Not IsArray($avArray) Then Return SetError(1, 0, 0) Local $iUBound = UBound($avArray, 1) - 1 If Not $iUBound Then Return SetError(2, 0, 0) Local $iDimension = UBound($avArray, 0) If $iDimension > 3 Then Return SetError(3, 0, 0) If ($iEnd > 0) And (($iUBound-$iEnd) < $iUBound) And (($iUBound-$iEnd) > $iStart) Then $iUBound -= $iEnd Local $iA, $vTmp For $i = $iStart To $iUBound ;Pick a random placement $iA = Random($iStart, $iUBound, 1) ;Do nothing for same value placements If $i = $iA Then ContinueLoop ;random fails at this point, get value of last element If $iA = 0 Then $iA = $iUBound Switch $iDimension Case 1 ;Store copy of 1st element value $vTmp = $avArray[$i] ;Overwrite 1st element value with 2nd element value $avArray[$i] = $avArray[$iA] ;Overwrite 2nd element value with 1st element value $avArray[$iA] = $vTmp Case 2 ;2D array For $x = 0 To UBound($avArray, $iDimension)-1 $vTmp = $avArray[$i][$x] $avArray[$i][$x] = $avArray[$iA][$x] $avArray[$iA][$x] = $vTmp Next EndSwitch Next Return 1 EndFunc ;==>_ArrayShuffleUpdated 2011/11/24 - fixed shuffling Edited November 24, 2011 by money Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now