Sign in to follow this  
Followers 0
PeterAtkin

mixed 2D arrays are they possible?

11 posts in this topic

#1 ·  Posted (edited)

I wish to make an array containing the below, but when ever i compile it I get an syntax error, a assume this is because I am mixing strings and numerics, but can not see why thats an issue as it is only an array do I have to somehow declare the variable type within the array? I have found no way to do that so fare what am I missing..

; $server[x][0] = hostname, $server[x][1] = ip address,
Global $serverArr[5][2] = [ _
  [scalaserver,10.0.0.1], _
  [serverbkuphq,10.0.0.4], _
  [carvm1,10.0.0.6], _
  [intranetserver,10.0.0.8], _
  [server1,10.0.0.9]]

Below if the function that i wish use with this array.

Func shutdown_servers()
For $x = 0 To $s Step 1
  $hostname = $serverArr[$x][1]
  MsgBox(64, "Shutting Down:", "remote system :" & $hostname, 1)
  $var = Ping($hostname, 250); is the server there? of course it may be blocking with a firewall so you need to check.
  If $var Then; also possible:  If @error = 0 Then ...
   RunAs($adminuser, $addomain, $adpassword, 0, @ComSpec & " /c " & "shutdown.exe -f -s -m \\" & $hostname & " -t 50 -c ""closing down"" ", @SystemDir, @SW_HIDE)
  Else
   MsgBox(16, "Server Status", "An error occured with :" & $hostname & @CRLF & @CRLF & "make sure the server is on or that the firewall allow pings" & @CRLF & "failing that give peter a call XXXX XXXXXXXX", 5)
  EndIf
Next
EndFunc   ;==>shutdown_servers

Any help in this would really be appreciated.

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You may wan to remove your #number from your post due to ident theft, just a friendly heads up!

Edited by Monolith

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

; $server[x][0] = hostname, $server[x][1] = ip address,
Global $serverArr[5][2] = [ _
  [scalaserver,10.0.0.1], _
  [serverbkuphq,10.0.0.4], _
  [carvm1,10.0.0.6], _
  [intranetserver,10.0.0.8], _
  [server1,10.0.0.9]]

Below is the function that i wish use with this array.

Looks like I answered my own question as to the arrays, seems after some guess work I came up with this:
[code]
; $server[x][0] = hostname, $server[x][1] = ip address,
Global $serverArr[5][2] = [ _
  [String("scalaserver"), Number("10.0.0.1")], _
  [String("serverbkuphq"), Number("10.0.0.4")], _
  [String("carvm1"), Number("10.0.0.6")], _
  [String("intranetserver"), Number("10.0.0.8")], _
  [String("server1"), Number("10.0.0.9")]]

However now is seems i need to rewrite this:

$hostname = $server[$x][1]

as now it comes up as an undeclared variable.

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites

You need to put each element of your array in speech marks.

Global $serverArr[5][2] = [ _
  ["scalaserver","10.0.0.1"], _
  ["serverbkuphq","10.0.0.4"], _
  ["carvm1","10.0.0.6"], _
  ["intranetserver","10.0.0.8"], _
  ["server1","10.0.0.9"]]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

OK all working, seems i had to add the $server as a Global Variable would love to know why?

I have attached the working script in the hope that someone else may find this useful to them, and for any advice people can give me for improving my understanding of Autoit.

rsd-working_array.au3

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

OK all working, seems i had to add the $server as a Global Variable would love to know why?

I have attached the working script in the hope that someone else may find this rsd-working_array.au3useful to them, and for any advice people can give me for improving my understanding of Autoit.

Shouldn't $server actually be $serverArr on line 204?

Edit: Yes it should, then you don't need a global $server variable because you don't actually use it anywhere other than that typo, once you change it you then don't need $server at all, also see my post abouve about declaring your array, you used String() but you don't need to

Edited by ChrisL

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks got it, New Year should never try and do anything just after new year, as to your post about array I do not see it?

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites

@Monolith where can you see my number becouse I cannot see it anywhere, I hate to be an idiot but then I suppose we all do.


[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thanks got it, New Year should never try and do anything just after new year, as to your post about array I do not see it?

2 replies above my last post!

When you declare your array you do it like this...

Global $serverArr[5][2] = [ _
  ["scalaserver","10.0.0.1"], _
  ["serverbkuphq","10.0.0.4"], _
  ["carvm1","10.0.0.6"], _
  ["intranetserver","10.0.0.8"], _
  ["server1","10.0.0.9"]]
Edited by ChrisL

Share this post


Link to post
Share on other sites

2 replies above my last post!

When you declare your array you do it like this...

Global $serverArr[5][2] = [ _
  ["scalaserver","10.0.0.1"], _
  ["serverbkuphq","10.0.0.4"], _
  ["carvm1","10.0.0.6"], _
  ["intranetserver","10.0.0.8"], _
  ["server1","10.0.0.9"]]

Thanks done, I finally managed to get the read file into array routine working so got rid of the manual array declaration, your comment really helped me in understanding arrays thank you.

Func read_host_file_into_array()
; host file sample (hosts.ini) placed on the desktop
If FileExists($hostfile) Then
  $var = IniReadSection($hostfile, "host_servers")
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
   For $i = 1 To $var[0][0]
    $serverArr[$i][1] = $var[$i][1]
    $serverArr[$i][0] = $var[$i][0]
    ; MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
   Next
   $sh = UBound($var) - 1; get zize of array just take some leg work out of the for next loops, i'm quit lazy
  EndIf
Else
  MsgBox(4096, "Host File Error", "The INI file does not exist.")
EndIf
EndFunc   ;==>read_host_file_into_array

Now all I have to do is work out had to dynamically resize the array as currently i declare the array as

Global $serverArr[99][2]
which seems very messy or at the very least wasteful.

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Now all I have to do is work out had to dynamically resize the array as currently i declare the array as

Global $serverArr[99][2]
which seems very messy or at the very least wasteful.

Something like this...

Global $$serverArr[1][2];Up at the top of the script where it usually is





Func read_host_file_into_array()
; host file sample (hosts.ini) placed on the desktop
If FileExists($hostfile) Then
  $var = IniReadSection($hostfile, "host_servers")
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
      
    Redim $serverArr[$var[0][0]+1][2]

   For $i = 1 To $var[0][0]
    $serverArr[$i][1] = $var[$i][1]
    $serverArr[$i][0] = $var[$i][0]
    ; MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
   Next
   $sh = UBound($var) - 1; get zize of array just take some leg work out of the for next loops, i'm quit lazy
  EndIf
Else
  MsgBox(4096, "Host File Error", "The INI file does not exist.")
EndIf
EndFunc   ;==>read_host_file_into_array
Edited by ChrisL

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