Jump to content
Sign in to follow this  
tes5884

Loop through an array

Recommended Posts

tes5884

The script is looping through a csv file with old pc names. It's then supposed to rename the pc's base on the service tag.

It seems to broken at the section where it loops through the array.

What am I doing wrong?

Thanks!!

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")

For $i = 0 To UBound($oPcName)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Next

Share this post


Link to post
Share on other sites
BrewManNH

I'm assuming when you say broken it means you're getting an array subscript error? If so, use For $i = 0 to Ubound($oPCName) - 1


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
tes5884

Tried that, still got error below.

ServiceTag.au3 (19) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Run(@ComSpec & " /k netdom renamecomputer " & ^ ERROR

The CSV file contains the following values;

tzvi-pc
test-pc

Thanks!

Share this post


Link to post
Share on other sites
jdelaney

Let me try this post again...

Do an _arraydisplay after the following line

$oPcName = _ParseCSV("test.csv")

send us back the output


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
tes5884

change this line:

For $i = 0 To UBound($oPcName) -1

Already did that per BrewManNH

Share this post


Link to post
Share on other sites
Jos

Well,

With the provided info that should fix it since your array only has 0 & 1 so shown in the arraydisplay.

Post the latest version of your script with the arraydisplay before the ForNext loop so we can see what you are testing with.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
BrewManNH

Add this after the line "For $i = 0 To UBound($oPcName) - 1"

ConsoleWrite("$i = " & $i & @CRLF)

Run your script from Scite and report the output from the console window at the bottom of the scite screen.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
tes5884

Add this after the line "For $i = 0 To UBound($oPcName) - 1"

ConsoleWrite("$i = " & $i & @CRLF)

Run your script from Scite and report the output from the console window at the bottom of the scite screen.

That worked!

$i = 0
$i = 1

Now why am I getting an error when I try to get the output of $oPcName[$i]

Share this post


Link to post
Share on other sites
Skruge

You have a 2-dimensional array, so you have to reference the elements differently.

UBound($oPcName) returns 2 because the array is 2-dimensional. (Not the row count, as you were expecting)

UBound($oPcName,1) also returns 2 because you have two lines in the CSV file.

Try this:

For $i = 0 To UBound($oPcName,1) -1
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i][0] & " /NewName:" & $nPcName & " /Force")
Next

Edit: Missed the -1

Edited by Skruge
  • Like 1

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
tes5884

This is the exact and entire script at this time.

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")
_ArrayDisplay($oPcName)

For $i = 0 To UBound($oPcName) -1
ConsoleWrite("$i = " & $i & @CRLF)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i] & " /NewName:" & $nPcName & " /Force")
Next

Share this post


Link to post
Share on other sites
tes5884

@Skruge it worked! Thank you all for helping!

This is the working edition if anyone else has the same problem..

#include <Constants.au3>
#include <Array.au3>
#include <CSV.au3>


$pid = Run("wmic bios get serialnumber", "", "", $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($pid)
$std = StdoutRead($pid)
$tag = StringRight(StringStripWS($std, 8), 7)


Local $dpt = "DPT"
Local $nPcName = $dpt & "-" & $tag


$oPcName = _ParseCSV("test.csv")
_ArrayDisplay($oPcName)

For $i = 0 To UBound($oPcName, 1)
ConsoleWrite("$i = " & $i & @CRLF)
Run(@ComSpec & " /k netdom renamecomputer " & $oPcName[$i][0] & " /NewName:" & $nPcName & " /Force")
Next
Edited by tes5884

Share this post


Link to post
Share on other sites
Mechaflash

The wicked two-headed array strikes again...


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
jdelaney

figured as much :) (mutli dim array, that is)

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
BrewManNH

I missed the Col1 in the array display, good catch.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Skruge

This is the working edition if has the same problem..

I'm not sure from your phrasing if you're still having issues, but the code you posted is missing the -1 after UBound. (I edited my post but it looks like you got the original)

If you still need help, let us know.

Cheers!


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
tes5884

I'm not sure from your phrasing if you're still having issues, but the code you posted is missing the -1 after UBound. (I edited my post but it looks like you got the original)

If you still need help, let us know.

Cheers!

Fixed. It works perfectly thanks..

This is the working edition if anyone else has the same problem..

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  

  • Similar Content

    • lattey
      By lattey
      hi,
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
       
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
       
      What is the best approach to accomplishing this method?
             
    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
×