Ghost1982 Posted March 15, 2020 Share Posted March 15, 2020 (edited) Hello I'm facing issue with "double" string splitting that's being passed into array. What I did was the string with set of addresses and their assigned IPs. What I want to have is to split the address and IP in the array but keeping them assigned to each other. Global $ipList = "www.address1.com:1.1.1.1|www.address2.com:2.2.2.2" ;etc.., - grouped addresses and their IPs delimited with ":", groups (address:IP) are separated with "|" Func array() $server = (FileRead("file.txt")) ;in file I've entered example: www.address1.com that is part of above string variable Global $array = StringSplit($ipList, "|") ;I'm entering the addresses into array and splitting with "|", now I have groups- address:IP that I can display _ArrayDisplay($array) If I've entered addresses group into array, but how can I than split them again with ":", so I can get IP as variable and keeping their alignement? What I want to achieve is i.e.: If Not @error Then MsgBox($MB_SYSTEMMODAL, "Server: ", $server & " found in array with IP " & $ip ) ;I don't know how to put separetly ip to another variable Else MsgBox($MB_SYSTEMMODAL, "Server: ", $server & " not found in array ") EndIf FileClose("file.txt") EndFunc Is there any way to split the string within the array rows? Or maybe replace part of the string. Everytime I was getting empty values. Edited March 16, 2020 by Ghost1982 Link to comment Share on other sites More sharing options...
Subz Posted March 15, 2020 Share Posted March 15, 2020 (edited) Just use a loop then split each array item, have them added to another array, basic example, although you could also do this with regexp. #include <Array.au3> Local $aServerList[0][2] Local $sServerList = "www.address1.com:1.1.1.1|www.address2.com:2.2.2.2" Local $aServers = StringSplit($sServerList, "|") For $i = 1 To $aServers[0] $aServer = StringSplit($aServers[$i], ":") If UBound($aServer) - 1 = 2 Then _ArrayAdd($aServerList, $aServer[1] & "|" & $aServer[2]) Next _ArrayDisplay($aServerList) Edited March 15, 2020 by Subz Ghost1982 1 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted March 15, 2020 Moderators Share Posted March 15, 2020 (edited) It sounds like (correct me if I am wrong) you're after an associative array, for which there is a pretty decent article on the Wiki. It discusses both using the Scripting Dictionary directly, as well as some UDFs that have been written around it: https://www.autoitscript.com/wiki/Associative_Arrays With the Scripting.Dictionary object you can do something like this: Local $oDictionary = ObjCreate("Scripting.Dictionary") $oDictionary.Add("www.address1.com", "1.1.1.1") $oDictionary.Add("www.address2.com", "1.1.1.2") $oDictionary.Add("www.address3.com", "1.1.1.3") For $key in $oDictionary.Keys ConsoleWrite($key & @CRLF) Next For $value in $oDictionary.Items ConsoleWrite($value & @CRLF) Next ConsoleWrite("The address for www.address1.com is " & $oDictionary("www.address1.com") & @CRLF) Edited March 15, 2020 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
UEZ Posted March 15, 2020 Share Posted March 15, 2020 #include <Array.au3> Global $ipList = "www.address1.com:1.1.1.1|www.address2.com:2.2.2.2|address3.de:10.100.200.254" $aResult = StringRegExp($ipList, "(\w.+?)\:(\d*\.\d*\.\d*\.\d*)", 3) _ArrayDisplay($aResult) Subz 1 Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
iamtheky Posted March 16, 2020 Share Posted March 16, 2020 (edited) _ArrayAdd will split it both ways #include <Array.au3> Local $aServerList[0][2] Local $sServerList = "www.address1.com:1.1.1.1|www.address2.com:2.2.2.2" _ArrayAdd($aServerList, $sServerList , 0 , ":" , "|") _ArrayDisplay($aServerList) Edited March 16, 2020 by iamtheky Subz and mikell 2 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Ghost1982 Posted March 16, 2020 Author Share Posted March 16, 2020 (edited) 21 hours ago, Subz said: Just use a loop then split each array item, have them added to another array, basic example, although you could also do this with regexp. #include <Array.au3> Local $aServerList[0][2] Local $sServerList = "www.address1.com:1.1.1.1|www.address2.com:2.2.2.2" Local $aServers = StringSplit($sServerList, "|") For $i = 1 To $aServers[0] $aServer = StringSplit($aServers[$i], ":") If UBound($aServer) - 1 = 2 Then _ArrayAdd($aServerList, $aServer[1] & "|" & $aServer[2]) Next _ArrayDisplay($aServerList) This helped a lot. However I got few more questions about array: - why we declare 0 rows in initial array instead already known number of rows? Local $aServerList[0][2] - what does the 0 index means in this part if we start from 1? For $i = 1 To $aServers[0] - why we always have to substract -1 from Ubound and why in ths case it must equal 2? If UBound($aServer) - 1 = 2 Edited March 16, 2020 by Ghost1982 Link to comment Share on other sites More sharing options...
Subz Posted March 16, 2020 Share Posted March 16, 2020 Why we declare 0 rows in initial array instead already known number of rows? You can declare the number of rows if you know how many rows there are going to be, I normally use 0 when the rows are dynamic. What does the 0 index means in this part if we start from 1? So StringSplit by default gives you the array count in the first row $aServers[0], unless you use something like: $aServer = StringSplit($aServers[$i], ":", $STR_NOCOUNT), which would mean starting the loop at 0 "For $i = 0 To Ubound($aServers) - 1" (Arrays are always 0 based). By starting at 1 we skip the first row (the count) and only process the rows below this. Why we always have to substract -1 from Ubound and why in ths case it must equal 2? As mentioned above (Arrays are 0 based) the Ubound($aServer) will count the number of rows in an array in this case there are 3 rows, ["count", "address", "ip address"], so we could have used "If Ubound($aServer) = 3" or as I did "If Ubound($aServer) - 1 = 2", both would be the same, I'm just use to the latter because its used when creating For loops when you don't have a count, for example to iterate through the $aServerList array we would use: "For $i = 0 To Ubound($aServerList) - 1". Technically because we used StringSplit without $STR_NOCOUNT we could have also used "If $aServer[0] = 2", this If statement is to check if the array is valid to add to the $aServerList array, i.e. does it have "address" and "ip address" in the array: $aServer[0] = 2 $aServer[1] = "www.address1.com" $aServer[2] = "1.1.1.1" Hope that made sense. Link to comment Share on other sites More sharing options...
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