Sign in to follow this  
Followers 0
kor

I do not understand loops at all.

6 posts in this topic

so I know what I want to do with the loops, i just dont know the code needed to make it happen. I have several scripts I am trying to set to preform AD functions from excel sheets. I have all the code I need to preform inside the loops, I just dont know how to make it loop through each excel row.

Example 1:

I have an excel sheet that has X rows with a set number of columns(lets say 10). I want to loop through each row and preform tasks, then mark a "complete" in the next available column (in this case column #11), then it needs to move on to the next row.

Example 2:

I have an 1 dimensional array of usernames from a query of all users in a particular OU. I need to loop through each one of those users from that array and preform tasks then move on to the next user.

In both examples I don't know how to create loops that do either of those things. Especially the array. If my array has 2 rows or 200 rows the script needs to continue until its done. but it needs to somehow take each row's value (the username) and preform tasks on that single user ignoring all the other users until it is done, then move on to the next user.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If you have problems with the programming fundamentals try looking for a programming book for beginners, it will sure help alot!

I will try to explain loops, not sure if it'll work:

Loops are used for repetitive tasks. Basically they stop when a condition is met, in example 2, the loop should stop when there are no more usernames left on the array to be processed.

I will use example 2 as my example:

;Let's say there are 10 usernames on the array
Local $array_position = 0
While $array_position < 10 ; while there are still unprocessed usernames on the array...
; process the username at $username_array[$array_position]
$array_position = $array_position + 1 ; moving to the next username
Wend ; This loop will have 10 cycles.
; After the last line of the instructions belonging to the loop is executed, 
;it goes back to the beginning and checks if $array_position<10 , if yes, it does another cycle,
;else it exits.

For example 1, you could use 2 loops, one nested in another. One for looping through the rows and another(inside the first one) to loop through the columns.

Hope it helps :x

Edited by pedmacedo

Share this post


Link to post
Share on other sites

for anyone hoping to make any program/script (or understand one) the concept of looping is both fundamental and critical. perhaps this may prove a useful read. Good luck :x

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You understand Arrays but not Loops?! I found when starting out that loops where easier to master than Arrays.

Local $Array[10] = [1,2,3,4,5,6,7,8,9,10]
For $i = 0 To Ubound($Array) - 1 ; <<<< Ubound() Tells how many items are in the Array in this case 10, but because we go from index 0 to index 9 we have to minus 1 from 10 to get the index 9!

   ConsoleWrite($Array[$i] & @CRLF) 
Next
Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

See, my problem is that the the arrays are always different sizes. I can't define a size because one time when the script runs there might be 10 rows, but another time there might be 5. Another time 26. Maybe if I give a code version of what I need the loops to do. I will provide screenshots as well.

Posted Image

Here is my excel sheet. In this case I have 6 items that need to be taken care of. However my script needs to do different things depending on if the "action" is new hire, termination, or transfer. I think that part I can work out myself, but getting that info into loops is beyond me. Here is the logical progression of what needs to happen.

Open Excel -> read data into array -> get 1 row worth of information at a time -> check action -> preform action

Here is my code so far on the excel example which I've managed to gather.

#include <excel.au3>
#include <array.au3>
 
$file = @ScriptDir & "\test.xls"
$oExcel = _ExcelBookOpen($file, 0) ; open excel in the background
 
$data = _ExcelReadSheetToArray($oExcel, 2) ; ignoring header row
_ArrayDelete($data, 0);removes the first row of array to get rid of count
_ArrayDisplay($data, "Array using Default Parameters")
 
For $i = 0 to UBound($data)-1
    $firstname = $data[$i][1]
    $lastname = $data[$i][2]
    $location = $data[$i][3]
    $title = $data[$i][4]
    $classification = $data[$i][5]
    $action = $data[$i][6]
    
    msgbox(0,'',$firstname)
Next
 
_ExcelBookClose($oExcel) ; close excel

My problems so far is that when the array displays, the data doesn't start at column 0 in the array. It starts at column 1. While annoying im not sure if that is how it is supposed to be.

Posted Image

-------------------------------------------------------------------------------------------------------------------------

Now my second issue is with loops of arrays. I'm sure the code to solve both of my problems is probably similar.

Posted Image

In this example I have 2 usernames. I get this array by doing the following:

#NoTrayIcon
#include <AD.au3>
#include <array.au3>

; Open connection to AD
_AD_Open()

Global $purgatory = "OU=Purgatory,DC=ad,DC=org,DC=org"

$users = _AD_GetObjectsInOU($purgatory, "(&(objectclass=user)(name=*))", 2, "sAMAccountName")

If $users = "" Then
    MsgBox(64, "No objects", "There are no objects in this OU")
    Exit
EndIf

_ArrayDelete($users, 0);removes the first row of array to get rid of count
_ArrayDisplay($users, "test")

_AD_Close()

What I need to do is cycle through the array and get the username of each account then do actions on that username before moving on to the next username. Again in this example I only have 2 rows, but I could have 10 rows, or 50 rows.

Edited by kor

Share this post


Link to post
Share on other sites

For example 2 I've tried this code and it seems to work. Would this be the correct way to do it?

For $i = 0 to UBound($users)-1
    $name = $users[$i]

    msgbox(0, '', $name)
Next

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