kor Posted July 14, 2011 Posted July 14, 2011 I have a tab deliminated file that I pull into an array. I then search to see if certain students are either enrolling, withdrawing, or moving. A move is a combination of a withdraw and an enroll. Our system will not let you "enroll" a student unless they have already been withdrawn. As such, a withdraw will always proceed an enroll. The two might not always be one right after the other.. there might be other students enrolling/withdrawing in between that time. I have attached my sample data. The code below does everything I need it to do with the except of modifying the array to do the following. If action = enroll then search the array (backwards) to see if there are any matching withdraws for this student (check is based on a duplicate ID number) source row = withdraw row destination row = enroll row If there is a corresponding withdrawn action for user with ID number you are checking then change action to = "move" in the destination row, and delete the source row. Loop, then spit out a new array. Picture 1 would be the first (raw) array Picture 2 SHOULD be what the fixed (clean) array looks like (but currently doesn't) Here is the full code: expandcollapse popup#NoTrayIcon #include <Array.au3> #include <File.au3> #include <String.au3> #include <AD.au3> ; Global variables Global $sWorkPath = "C:\users\hdk\desktop" Global $sBackupPath = $sWorkPath & "\backup" ; backup directory Global $sCSV = $sWorkPath & "\test1.txt" Global $iRowStart = 0 ; starting row => 0=no header row, 1=header row Global $sPassword = "student" ; password to use when creating student accounts Global $aRaw, $bValid, $aClean, $bUserExists, $sOU, $sUsername, $sStudentGroup ; Copy output to backup folder before running FileCopy($sCSV, $sBackupPath & "\cycle-" & @MON & "-" & @MDAY & "-" & @YEAR & "--" & @HOUR & "-" & @MIN & ".txt", 8) ; cycle-07-13-2011--14-55.txt _FormatCSV($sCSV) ; take CSV and convert to array rows and columns _SetAction($aRaw) Func _FormatCSV($sCSV) $sCSV = FileRead($sCSV) ; read csv If @error = 1 Then _ExitWithError("Error : Unable to open CSV file") ;$sCSV = StringStripWS($sCSV, 7) ; remove any whitespaces Local $iRows = StringSplit($sCSV, @CR) ; make each line a new row Global $aRaw[$iRows[0] + 1][9] ; create raw array with dimensions based on rows For $i = 1 To $iRows[0] Local $aTemp = StringSplit($iRows[$i], @TAB, 2) ; create temp array to split all values into For $n = 0 To UBound($aTemp) - 1 $aRaw[$i][$n] = $aTemp[$n] ; fill array with split data Next Next _ArrayDelete($aRaw, 0) ; delete row count in element 0 _ArrayDisplay($aRaw, "araw") EndFunc ;==>_FormatCSV #comments-start ;_ValidateData($aRaw) ; check that array is complete and does not contain null data ;If $bValid = True Then _SetAction($aRaw) ; logic to check for moves Func _ValidateData(ByRef $aInput) Global $bValid = False For $i = $iRowStart To UBound($aInput) - 1 ; 1d loop For $n = 0 to UBound($aInput, 2) - 1 ; 2d loop If $aInput[$i][$n] = "" Then ; check if any element is blank _ExitWithError("Error : Array contains null data") Else $bValid = True EndIf Next Next EndFunc ;==>_ValidateData #comments-end Func _SetAction(ByRef $aInput) For $i = UBound($aInput) - 1 To $iRowStart Step -1 ; read array backwards Switch $aInput[$i][5] ; check action Case "enroll" $iReturn = _ArraySearch($aInput, $aInput[$i][2]) ; search array for duplicate ID numbers If $iReturn > 0 And $iReturn < $i Then ; if ID numbers found and row is less than current row If $aInput[$iReturn][2] = $aInput[$i][2] Then ; compare source row ID number against destination row $aInput[$i][8] = "move" ; modify action on destination row _ArrayDelete($aInput, $iReturn) ; delete source row EndIf EndIf EndSwitch Next Global $aClean = $aInput ; create new array and fill with clean data _ArrayDisplay($aInput, "ainput") EndFunc ;==>_SetActiontest.txt
taietel Posted July 14, 2011 Posted July 14, 2011 I've modified your function a little: Func _SetAction(ByRef $aInput) For $i = UBound($aInput)-1 To $iRowStart Step -1 ; read array backwards Switch $aInput[$i][8] ; check action Case "Enroll" $iReturn = _ArraySearch($aInput, $aInput[$i][2]) ; search array for duplicate ID numbers If $iReturn >= 0 And $iReturn < $i Then ; if ID numbers found and row is less than current row If $aInput[$iReturn][2] = $aInput[$i][2] Then ; compare source row ID number against destination row $aInput[$i][8] = "move" ; modify action on destination row _ArrayDelete($aInput, $iReturn) ; delete source row EndIf EndIf EndSwitch Next Global $aClean = $aInput ; create new array and fill with clean data _ArrayDisplay($aInput, "ainput") EndFunc ;==>_SetAction Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text
jaberwacky Posted July 14, 2011 Posted July 14, 2011 (edited) This might be what you're after: expandcollapse popup#NoTrayIcon #include <Array.au3> #include <File.au3> #include <String.au3> #include <AD.au3> ; Global variables Global $sWorkPath = "C:\users\hdk\desktop" Global $sBackupPath = $sWorkPath & "\backup" ; backup directory Global $sCSV = $sWorkPath & "\test1.txt" Global $iRowStart = 0 ; starting row => 0=no header row, 1=header row Global $sPassword = "student" ; password to use when creating student accounts Global $aRaw, $bValid, $aClean, $bUserExists, $sOU, $sUsername, $sStudentGroup ; Copy output to backup folder before running FileCopy($sCSV, $sBackupPath & "\cycle-" & @MON & "-" & @MDAY & "-" & @YEAR & "--" & @HOUR & "-" & @MIN & ".txt", 8) ; cycle-07-13-2011--14-55.txt _FormatCSV($sCSV) ; take CSV and convert to array rows and columns _SetAction($aRaw) Func _FormatCSV($sCSV) $sCSV = FileRead($sCSV) ; read csv If @error = 1 Then _ExitWithError("Error : Unable to open CSV file") ;$sCSV = StringStripWS($sCSV, 7) ; remove any whitespaces Local $iRows = StringSplit($sCSV, @CR) ; make each line a new row Global $aRaw[$iRows[0] + 1][9] ; create raw array with dimensions based on rows Local $aTemp For $i = 1 To $iRows[0] $aTemp = StringSplit($iRows[$i], @TAB, 2) ; create temp array to split all values into For $n = 0 To UBound($aTemp) - 1 $aRaw[$i][$n] = $aTemp[$n] ; fill array with split data Next Next _ArrayDelete($aRaw, 0) ; delete row count in element 0 _ArrayDisplay($aRaw, "araw") EndFunc ;==>_FormatCSV #comments-start ;_ValidateData($aRaw) ; check that array is complete and does not contain null data ;If $bValid = True Then _SetAction($aRaw) ; logic to check for moves Func _ValidateData(ByRef $aInput) Global $bValid = False For $i = $iRowStart To UBound($aInput) - 1 ; 1d loop For $n = 0 to UBound($aInput, 2) - 1 ; 2d loop If $aInput[$i][$n] = "" Then ; check if any element is blank _ExitWithError("Error : Array contains null data") Else $bValid = True EndIf Next Next EndFunc ;==>_ValidateData #comments-end Func _SetAction(ByRef $aInput) Local $iReturn Local Const $upbound = UBound($aInput) - 1 For $i = $upbound To $iRowStart Step - 1 ; read array backwards Switch $aInput[$i][8] ; check action Case "enroll" $iReturn = _ArraySearch($aInput, $aInput[$i][2]) ; search array for duplicate ID numbers If $iReturn >= 0 And $iReturn < $i Then ; if ID numbers found and row is less than current row If $aInput[$iReturn][2] = $aInput[$i][2] Then ; compare source row ID number against destination row $aInput[$i][8] = "move" ; modify action on destination row _ArrayDelete($aInput, $iReturn) ; delete source row EndIf EndIf EndSwitch Next Global $aClean = $aInput ; create new array and fill with clean data _ArrayDisplay($aInput, "ainput") EndFunc ;==>_SetAction Blast! Beaten by taietel Edited July 14, 2011 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
kor Posted July 14, 2011 Author Posted July 14, 2011 thank you. That was simple once I see what you changed.
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