Sign in to follow this  
Followers 0
computergroove

I need help documenting some code generously donated to me today

6 posts in this topic

#1 ·  Posted (edited)

I have successfully written several smaller programs and I love Autoit. I was given some code from someone on here and I need to manipulate it and I am having a hard time understanding what is happening in some of it. Can someone help me understand the logic of the code in the following please?

I think I broke this one down OK.

Func CreateRandomTemp();Donated by UEZ 2014
    Local $sString = FileRead(@ScriptDir & "\Video.ini");local to this function. Read Video.ini to $sString
    Local $2D_Array = StringSplitW($sString, "=");splits the data in Videos.ini seperated by the = sign.
    Shuffle_Array($2D_Array);Randomizes the video file names
    Local $hFile = FileOpen(@ScriptDir & "\RandomizeTemp.ini", 2);opens RandomizeTemp.ini for modifying
    FileWrite($hFile, _Array2DToString($2D_Array, "="));Writes new random files to RandomizeTemp.ini
    FileClose($hFile);Closes teh file after editing is done
EndFunc

I don't understand this at all

Func _Array2DToString($array, $sDelimiter = ";") ;coded by UEZ 2014
    If Not IsArray($array) Then Return SetError(1, 0, 0)
    If UBound($array, 0) > 2 Then Return SetError(2, 0, 0) ;up to 2D arrays only
    Local $iW, $iH, $sString
    For $iH = 0 To UBound($array) - 1
        For $iW = 0 To UBound($array, 2) - 1
            $sString &= $array[$iH][$iW] & $sDelimiter
        Next
        $sString = ($iH < UBound($array) - 1) ? StringTrimRight($sString, 1) & @CRLF : StringTrimRight($sString, 1)
    Next
    Return $sString
EndFunc

Basically what is happening is I am feeding a ini file with a barcode and a video file to an array and the above is shuffeling the result and writing it to a different temp.ini. The problem is that I want to get rid of the barcode and "=" in the new file created.

Video.ini looks like this:

123456=Video1.mpg

2345=Video2.mpg

32136549872132=Video3.mpg

etc

I want to erase all the info from the "=" going left so that what remains is video1.mpg or whatever the name of the video file is. Where can I do this in te provided code?

 

Heres the rest of the important stuff:


; #FUNCTION# ========================================================================================================================================
; Name .................:   StringSplitW()
; Description ..........:   Splits  a string into columns instead of rows as it is done by SplitString(), like a csv file to a 2d array ;-)
; Syntax ...............:   StringSplitW($sString, $sDelimiter, $iWidthLen)
; Parameters ...........:   $sString - string to split
;                           $sDelimiter - [optional] the delimter how to split the string
;                           $iWidthLen - [optional] length of the row (amount of columns - default is 100)
; Return values .......:    Success - 2d array
;                           Error 1 - either $sString or $delimter is not set
;                           Error 2 - array width exceeded
;                           Error 3 - error splitting string
;
; Version .............:    v0.93 build 2013-08-23 beta
; Author ..............:    UEZ
; Modified ............:
; Remarks .............:
; Related .............:    StringSplit()
; ===================================================================================================================================================
Func StringSplitW($sString, $sDelimiter = ";", $iWidthLen = 256)
    If $sString = "" Or $sDelimiter = "" Then Return SetError(1, 0, 0)
    Local $chk, $iWidth, $i, $j, $k, $iLen, $iMax = 1, $iMaxWidth
    Local $aPos[1], $l = 0
    Local $aSplit =  StringSplit(StringStripCR($sString), @LF)
    If @error Then Return SetError(3, 0, 0)
    Local $aVertical[$aSplit[0]][$iWidthLen], $iDelimiterLen = StringLen($sDelimiter) - 1
    For $k = 1 To $aSplit[0]
        $iLen = StringLen($aSplit[$k])
        If $iLen > 1 Then
            $chk = StringReplace($aSplit[$k], $sDelimiter, $sDelimiter)
            $iWidth = @extended
            If $iWidth > $iWidthLen Then Return SetError(2, 0, 0)
            If $iWidth >= $iMax Then $iMax = $iWidth + 1
            Switch $iWidth
                Case 0
                    $aVertical[$l][0] = $aSplit[$k]
                Case Else
                    Dim $aPos[$iWidth * 2 + 2]
                    $j = 1
                    $aPos[0] = 1
                    For $i = 0 To $iWidth - 1
                        $aPos[$j] = StringInStr($aSplit[$k], $sDelimiter, 0, $i + 1) - 1
                        $aPos[$j + 1] = $aPos[$j] + 2 + $iDelimiterLen
                        $j += 2
                    Next
                    $aPos[UBound($aPos) - 1] = StringLen($aSplit[$k])
                    $j = 0
                    For $i = 0 To UBound($aPos) - 1 Step 2
                        $aVertical[$l][$j] = StringMid($aSplit[$k], $aPos[$i], $aPos[$i + 1] - $aPos[$i] + 1)
                        $j += 1
                    Next
                EndSwitch
                $l += 1
        EndIf
    Next
    ReDim $aVertical[$l][$iMax]
    Return $aVertical
EndFunc

; #FUNCTION# ======================================================================================
; Name ................:    Shuffle_Array()
; Version .............:    v0.50 build 2011-05-24 beta
; Description .......:  Shuffles an array - support 1D and 2D arrays only
; Syntax ..............:    Shuffle_Array(ByRef $array, $startindex = 0, $endindex = 0)
; Parameters ........:  $array - the array to shuffle
;                               $startindex = from which index to start the shuffling
;                               $endindex = to which index to start the shuffling; 0 means last index of the array
; Return values ....:   True
;                               Failure 1 - $array is not an array
;                               Failure 2 - array has more than 2 dimensions
;                               Failure 3 - array is empty
;                               Failure 4 -  $startindex / $endindex are set wrongly
; Author ..............:    UEZ
; Modified ............:
; Remarks ............:
; Related ..............:   Array
; =================================================================================================
Func Shuffle_Array(ByRef $array, $startindex = 0, $endindex = 0)
    If Not IsArray($array) Then Return SetError(1, 0, 0)
    If UBound($array, 0) > 2 Then Return SetError(2, 0, 0)
    If UBound($array) = 1 Then Return SetError(3, 0, 0)
    Local $u1
    If Not $endindex Then
        $u1 = UBound($array) - 1
    Else
        If $endindex > $startindex And $endindex < UBound($array) Then
            $u1 = $endindex
        Else
            Return SetError(4, 0, 0)
        EndIf
    EndIf
    If UBound($array, 2) Then
        Local $aSwap[1][UBound($array, 2)], $u2 = UBound($array, 2) - 1
        Local $i, $j, $r
        For $i = $startindex To $u1
            $r = Random($startindex, $u1, 1)
            For $j = 0 To $u2
                $aSwap[0][$j] = $array[$i][$j]
                $array[$i][$j] = $array[$r][$j]
                $array[$r][$j] = $aSwap[0][$j]
            Next
        Next
    Else
        Local $aSwap[1]
        For $i = $startindex To $u1
            $r = Random($startindex, $u1, 1)
            $aSwap[0] = $array[$i]
            $array[$i] = $array[$r]
            $array[$r] = $aSwap[0]
        Next
    EndIf
    Return 1
EndFunc
Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



Why not make that simple? Using the beta:

Local $aVids = StringRegExp(FileRead("Video.ini"), "(?<==).*", 3)
_ArrayShuffle($aVids)
_FileWriteFromArray("RandomizeTemp.ini", $aVids, Default, Default, @CRLF)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

computergroove,

you are member since May 2008 and you have still lacks of understanding for arrays. My suggestion is that you study e.g. in the help file everything about arrays.

At the beginning you asked how to shuffle an ini file which looks like

83948100026=Annihilator.wmv
83948100685=bouquet_of_mines.wmv
83948100263=Dragon_Slayer.wmv
83948100657=fearsome_finale.wmv
83948100691=four_times_as_fun.wmv
8394810021400234=Furious_Fireworks.wmv
83948100658=Gargantuan.wmv
83948100692=Glitters.wmv
...

and now you ask how to delete everything left to "=" including "=". In this case you can simplify everything and use jchd's code but probably you will ask what "StringRegExp(FileRead("Video.ini"), "(?<==).*", 3)" is doing.

 

Here some additions 

Func CreateRandomTemp();Donated by UEZ 2014
    Local $sString = FileRead(@ScriptDir & "\Video.ini");local to this function. Read Video.ini to $sString
    Local $2D_Array = StringSplitW($sString, "=");splits the data in Videos.ini seperated by the = sign and create a 2D array
    Shuffle_Array($2D_Array);Randomizes the video file names
    Local $hFile = FileOpen(@ScriptDir & "\RandomizeTemp.ini", 2);opens RandomizeTemp.ini (write mode (erase previous contents))
    FileWrite($hFile, _Array2DToString($2D_Array, "="));Writes new random files to RandomizeTemp.ini
    FileClose($hFile);Closes teh file after editing is done
EndFunc

and here my comments to _Array2DToString:

Func _Array2DToString($array, $sDelimiter = ";") ;coded by UEZ 2014
    If Not IsArray($array) Then Return SetError(1, 0, 0) ;check whether $array is an array
    If UBound($array, 0) > 2 Then Return SetError(2, 0, 0) ;up to 2D arrays only
    Local $iW, $iH, $sString
    For $iH = 0 To UBound($array) - 1 ;travers the 2D row by row
        For $iW = 0 To UBound($array, 2) - 1 ;travers the 2D column by column
            $sString &= $array[$iH][$iW] & $sDelimiter ;and create a string with the content of the array
        Next
        $sString = ($iH < UBound($array) - 1) ? StringTrimRight($sString, 1) & @CRLF : StringTrimRight($sString, 1)
;~      the line above is equal to:
;~      If $iH < UBound($array) - 1 Then ;while end of $iH (row) is not reached remove delimter at the end of the line and add @CRLF (line break)
;~          $sString = StringTrimRight($sString, 1) & @CRLF 
;~      Else
;~          $sString = StringTrimRight($sString, 1) ;if end has been reached only remove delimter
;~      EndIf
    Next
    Return $sString
EndFunc

I know that the function naming of function StringSplitW() is not quite correct, should be rather something like CSVtoArray.

What you can do also

...
Func CreateRandomTemp()
    Local $sString = FileRead(@ScriptDir & "\Video.ini")
    Local $2D_Array = StringSplitW($sString, "=")
    Shuffle_Array($2D_Array)
    Local $hFile = FileOpen(@ScriptDir & "\RandomizeTemp.ini", 2)
    FileWrite($hFile, _ArrayToString2($2D_Array, "=", 1))
    FileClose($hFile)
EndFunc

Func _ArrayToString2($array, $sDelimiter = ";", $iColumn = 0) ;coded by UEZ 2014
    If Not IsArray($array) Then Return SetError(1, 0, 0)
    If UBound($array, 0) > 2 Then Return SetError(2, 0, 0) ;up to 2D arrays only
    Switch UBound($array, 0) ;check whether $iColumn is within the boundary
        Case 1  ;for 1D array
            If $iColumn > 0 Then Return SetError(3, 0, 0)
        Case Else ;else for 2D array
            If $iColumn > UBound($array, 2) - 1 Then Return SetError(3, 0, 0)
    EndSwitch
    Local $iW, $iH, $sString
    For $iH = 0 To UBound($array) - 1 ;generate string from array on selected column
        $sString &= $array[$iH][$iColumn] & @CRLF
    Next
    Return $sString
EndFunc

...

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

 

Why not make that simple? Using the beta:

Local $aVids = StringRegExp(FileRead("Video.ini"), "(?<==).*", 3)
_ArrayShuffle($aVids)
_FileWriteFromArray("RandomizeTemp.ini", $aVids, Default, Default, @CRLF)

I installed the beta version of autoit and I am getting an error about _ArrayShuffle() being an undefined function when I try to compile it.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Are you compiling for the beta (Alt F7) or the release (F7)?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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  
Followers 0