feelie75 Posted July 6, 2007 Share Posted July 6, 2007 Ok, so I'm very familiar with arrays, but not AutoIt... I have a 4x4 grid: ABCD FJAP FMEK FOOB What's the quickest way to get those into a 4x4 array? I tried this: Dim $board[4][4] = [[stringsplit("ABCD","")],[stringsplit("FJAP","")],[stringsplit("FMEK","")],[stringsplit("FOOB","")]] that doesn't work obviously... I could do [["A","B","C","D"],[..... but I just sorta wanted all the letters to line up. I mean, here's the deal, in PERL I can do this: @array = qw( join(" ",split('',"ABCD")), join(" ",split('',"FJAP")), join(" ",split('',"FMEK")), join(" ",split('',"FOOB"))); ok, so i guess that's not very pretty, but on a fixed-width font the board 4x4 grid looks good in the code, heh... Also I noticed I can't split my Dim $array[4][4] across multiple lines??? I had to put it all on one line. is that right? odd. I guess a related question would be what if I have a nicely formatted string like so: $string = "ABCD" & "FJAP" & "FMEK" & "FOOB" can I convert that into a 4x4 array gracefully? Can I even create a string like that so it looks good in code? Sorry if I'm convoluting things Just trying to make my code as pretty as possible! Thanks! Link to comment Share on other sites More sharing options...
Siao Posted July 6, 2007 Share Posted July 6, 2007 (edited) Edit: Bleh... Sorry, misunderstood OP Edited July 6, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
poisonkiller Posted July 6, 2007 Share Posted July 6, 2007 Based on Siao's version: #include <Array.au3> Dim $board[4][4] = [["A","B","C","D"],["F","J","A","P"],["F","M","E","K"],["F","O","O","B"]] _ArrayDisplay($board) Link to comment Share on other sites More sharing options...
Siao Posted July 6, 2007 Share Posted July 6, 2007 (edited) #include <array.au3> $string = "ABCD" & "FJAP" & "FMEK" & "FOOB" & "BOOKA!" $grid = _Str2Array($string, 4) _ArrayDisplay($grid) Func _Str2Array($string, $rowsize) Local $size = Ceiling(StringLen($string) / $rowsize) Local $a[$size][$rowsize] For $i = 0 to $size-1 For $j = 0 to $rowsize-1 $a[$i][$j] = StringMid($string, $i*$rowsize+$j+1, 1) Next Next Return $a EndFunc Edited July 6, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
martin Posted July 6, 2007 Share Posted July 6, 2007 Ok, so I'm very familiar with arrays, but not AutoIt... I have a 4x4 grid: ABCD FJAP FMEK FOOB What's the quickest way to get those into a 4x4 array? I tried this: Dim $board[4][4] = [[stringsplit("ABCD","")],[stringsplit("FJAP","")],[stringsplit("FMEK","")],[stringsplit("FOOB","")]] that doesn't work obviously... I could do [["A","B","C","D"],[..... but I just sorta wanted all the letters to line up. I mean, here's the deal, in PERL I can do this: @array = qw( join(" ",split('',"ABCD")), join(" ",split('',"FJAP")), join(" ",split('',"FMEK")), join(" ",split('',"FOOB"))); ok, so i guess that's not very pretty, but on a fixed-width font the board 4x4 grid looks good in the code, heh... Also I noticed I can't split my Dim $array[4][4] across multiple lines??? I had to put it all on one line. is that right? odd. I guess a related question would be what if I have a nicely formatted string like so: $string = "ABCD" & "FJAP" & "FMEK" & "FOOB" can I convert that into a 4x4 array gracefully? Can I even create a string like that so it looks good in code? Sorry if I'm convoluting things Just trying to make my code as pretty as possible! Thanks! I might not be understanding the question correctly, but if you mean "how in general can I write a string in a neat way that can be converted to a 4 x 4 array in my code?" then $string ="ABCD|FJAP|FMEK|FOOB" Dim $BOARD[4][4] $LEVEL1 = StringSplit($STRING,'|') For $n = 0 To 3 For $p = 0 To 3 $BOARD[$n][$p] = StringMid($level1[$n+1],$p + 1,1) Next Next But if you just want to write the string so that it will be printed in 4 lines then $string = Stringreplace("ABCD|FJAP|FMEK|FOOB","|",'" & @CRLF & "') Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
feelie75 Posted July 6, 2007 Author Share Posted July 6, 2007 #include <array.au3> $string = "ABCD" & "FJAP" & "FMEK" & "FOOB" & "BOOKA!" $grid = _Str2Array($string, 4) _ArrayDisplay($grid) Func _Str2Array($string, $rowsize) Local $size = Ceiling(StringLen($string) / $rowsize) Local $a[$size][$rowsize] For $i = 0 to $size-1 For $j = 0 to $rowsize-1 $a[$i][$j] = StringMid($string, $i*$rowsize+$j+1, 1) Next Next Return $a EndFunc Ahh excellent! yep, I was a little confused by your first reply, heh. Yeah this will work great. I can space out the $string = into 4 lines and line up the groups of letters for better readability. Actually it'll be for better editing so that I can retype the grid into the $string variable when I need to change it. Thanks! This will make my code much easier to change the grid... incase you're wondering, I'm writing a Minesweeper solving utility . I've got it working so far so that it'll take a 10x10 grid of various "solid", "numbered" and "open space" squares and it will figure out where all the mines are. There's a chance that one or more mines may be guesswork though but it seems to solve between 80-100% of them so far. thanks again! (assuming it works, hah, I haven't pasted it in yet ) Link to comment Share on other sites More sharing options...
feelie75 Posted July 6, 2007 Author Share Posted July 6, 2007 I might not be understanding the question correctly, but if you mean "how in general can I write a string in a neat way that can be converted to a 4 x 4 array in my code?" then $string ="ABCD|FJAP|FMEK|FOOB" Dim $BOARD[4][4] $LEVEL1 = StringSplit($STRING,'|') For $n = 0 To 3 For $p = 0 To 3 $BOARD[$n][$p] = StringMid($level1[$n+1],$p + 1,1) Next Next But if you just want to write the string so that it will be printed in 4 lines then $string = Stringreplace("ABCD|FJAP|FMEK|FOOB","|",'" & @CRLF & "') Ah, that sounds like a reasonable approach to it also. Yeah, that's what I wanted to do. Convert a nice looking string (I'd split that $string= into 4 lines to align the columns to each other) into a [][] array. I'll try both approaches and see which one I like best 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