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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                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

    • rm4453
      By rm4453
      Hello,
       
      I am currently writing a program that parses a massive table from a website, and need a way to add a progress bar while parsing.
      I am currently using the function _IETableWriteToArray($oObj, True) to parse the array. I need the progress bar to update as the table is parsed, not just at the end of the parsing.
      Any help at all would be very much appreciated!
       
      *EDIT --> The array I am left with after parsing is $array[0-50000][16]
    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
       
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

       
    • ternal
      By ternal
      Hi,
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
    • TrashBoat
      By TrashBoat
      So I've made this script that detects how long i have held down my left mouse button for and stores the information in an array and then sorts its using _ArraySort but the output is half sorted half broken.
      Here's my script:
      HotKeySet("{F1}","_exit") #include <Misc.au3> #include <Timers.au3> #include <Array.au3> Local $dll = DllOpen("user32.dll") $on = False Global $array[0] While(1) If _IsPressed(01,$dll) Then $timer = _Timer_Init() While _IsPressed(01,$dll) Sleep(1) WEnd $time = _Timer_Diff($timer) _ArrayAdd($array,"Time: " & Floor($time) & " ms") ;~ ConsoleWrite("Time: " & Floor($time) & " ms" & @CRLF) EndIf Sleep(50) WEnd Func _exit() _ArraySort($array) _ArrayDisplay($array) Exit EndFunc And the output:

      See how its not sorted?  What is the problem here?
×