Jump to content

1D to 2D Array Issues


 Share

Recommended Posts

Hello folks,

Think I just posted this in the wrong spot.....Am I able to move it or MOD job?

I am still trying to learn arrays. I have read the Wiki and posts here to try and suck up all I could before posting.....

So the problem im having is that I can use my script to query a unit im working on, get the info from the unit, and place it into a 1d array. Now I need to get that info into a 2d array, <Not always sure how large the 1d array is going to be so I need to figure out how to create the 2d array dynamically Just using [50][50] as a placeholder on size> . This is going to be part of a larger project that allows me to show the switch positions in the units with out querying each one. attached is the code and also a screen shot of the 1d arraydisplay.

Just looking for hints/clues as to how to accomplish this. 

#include <Array.au3>
#include <ButtonConstants.au3>
#include <CommInterface.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <ColorConstants.au3>
#include <GuiRichEdit.au3> ; Color Notes ...: Green = 65280 , Red = 255 , Blue = 16711680

#Region ### START Koda GUI section ### Form=k:\work\autoit scripts\mine\models\testing\showset.kxf
$g_Core = GUICreate("Show Set Data Test", 615, 437, 192, 124)
$i_Model = GUICtrlCreateInput("", 16, 16, 121, 21)
$i_CommPort = GUICtrlCreateInput("", 144, 16, 121, 21)
$b_Connect = GUICtrlCreateButton("", 272, 16, 75, 25)
$b_ShowSet = GUICtrlCreateButton("", 352, 16, 75, 25)
$g_hRichEdit = _GUICtrlRichEdit_Create($g_Core, "", 16, 48, 585, 361, _
        BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY))
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetData(-1, "")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetData($b_Connect, "Connect")
GUICtrlSetData($b_ShowSet, "Show Set")

_DisplayPorts($g_hRichEdit)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $b_Connect
            $sUnit = GUICtrlRead($i_Model)
            $sCommPort = GUICtrlRead($i_CommPort)

            Local $aArray = IniReadSection(@ScriptDir & "\unitinfo.ini", $sUnit)

            ;Local Const $iPort = "" & IniRead(@ScriptDir & "\unitinfo.ini", $sUnit, "Port", "5") & "" ;IniRead ( "filename", "section", "key", "default" )
            Local Const $iBaud = "" & IniRead(@ScriptDir & "\unitinfo.ini", $sUnit, "Baud", "11500") & "" ;IniRead ( "filename", "section", "key", "default" )
            Local Const $iParity = "" & IniRead(@ScriptDir & "\unitinfo.ini", $sUnit, "Parity", "0") & "" ;IniRead ( "filename", "section", "key", "default" )
            Local Const $iByteSize = "" & IniRead(@ScriptDir & "\unitinfo.ini", $sUnit, "ByteSize", "8") & "" ;IniRead ( "filename", "section", "key", "default" )
            Local Const $iStopBits = "" & IniRead(@ScriptDir & "\unitinfo.ini", $sUnit, "StopBits", "1") & "" ;IniRead ( "filename", "section", "key", "default" )

            ;Global Const $hFile = _CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
            ;Global $hFile = _CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
            Global $hFile = _CommAPI_OpenCOMPort($sCommPort, $iBaud, $iParity, $iByteSize, $iStopBits)

            _CommAPI_TransmitString($hFile, "*IDN?" & @CR) ; _CommAPI_TransmitData(Const $hFile, Const $sData)
            Local $sResult = _CommAPI_ReceiveString($hFile, 1000) ;_CommAPI_ReceiveString(Const $hFile[, $iTimeout = 1[, $iMaxLength = 0[, $sSeparator = ""]]])

            _GUICtrlRichEdit_SetCharColor($g_hRichEdit, 16711680)
            _GUICtrlRichEdit_AppendText($g_hRichEdit, $sResult)
            _GUICtrlRichEdit_AppendText($g_hRichEdit, "---------------------------------------" & @CRLF)
        Case $b_ShowSet
            _CommAPI_ClearCommError($hFile)
            If @error Then Exit SetError(@error, @ScriptLineNumber)

            _CommAPI_PurgeComm($hFile)
            If @error Then Exit SetError(@error, @ScriptLineNumber)

            _CommAPI_TransmitString($hFile, "SHOW Stat" & @CR) ; _CommAPI_TransmitData(Const $hFile, Const $sData) - Works for show set and show stat
            If @error Then Exit SetError(@error, @ScriptLineNumber)

            Sleep(500)

            Local $sResult = _CommAPI_ReceiveString($hFile, 1000) ;_CommAPI_ReceiveString(Const $hFile[, $iTimeout = 1[, $iMaxLength = 0[, $sSeparator = ""]]])
            If @error Then Exit SetError(@error, @ScriptLineNumber, $sResult)

            _GUICtrlRichEdit_SetCharColor($g_hRichEdit, 16711680)
            _GUICtrlRichEdit_AppendText($g_hRichEdit, $sResult)
            _GUICtrlRichEdit_AppendText($g_hRichEdit, "---------------------------------------" & @CRLF)

            ; Show Set split to 1D Array
            _ShowArray($sResult)
    EndSwitch
WEnd

; #FUNCTION# ==============================================================================================
; Name ..........: _DisplayPorts
; =========================================================================================================
Func _DisplayPorts($g_hRichEdit)
    Local $sCommPorts = _CommAPI_GetCOMPorts() ; ref https://www.autoitscript.com/wiki/CommAPI_Examples
    Local $uCommPorts = "COM Ports:" & @LF & $sCommPorts & @LF
    _GUICtrlRichEdit_AppendText($g_hRichEdit, "---------------------------------------" & @CRLF & @CRLF)
    _GUICtrlRichEdit_SetCharColor($g_hRichEdit, 16711680)
    _GUICtrlRichEdit_AppendText($g_hRichEdit, $uCommPorts)
    _GUICtrlRichEdit_AppendText($g_hRichEdit, "---------------------------------------" & @CRLF)

EndFunc   ;==>_DisplayPorts

; #FUNCTION# ==============================================================================================
; Name ..........: _ShowArray($sResult)
; =========================================================================================================
Func _ShowArray($sResult)
    Local $srString = StringReplace($sResult, @CRLF, "|")
    Global $aResult = StringSplit($srString, "|" & ":")
    Global $aResult2d[50][2]

    For $i = UBound($aResult) - 1 To 0 Step -1
        If $aResult[$i] = "" Then _ArrayDelete($aResult, $i)
    For $j = 0 To UBound($aResult2d, 2) - 1
        $aResult2d[$i][$j] = ($aResult[$j + 1]) ; Get the elements into the 2D array
    Next
    Next

    _ArrayDisplay($aResult, "Possible Show Set Array Split")

    Global $aResult2d[43][43]


_ArrayDisplay($aResult2d)



EndFunc   ;==>_ShowArray

 

ArrayIssues.png

Edited by Fractured
Possible post location error
Link to comment
Share on other sites

It could be of great help to post the "$sResult" string ...

What about something like this ?

Func _ShowArray($sResult)
    Local $srString = StringReplace($sResult, @CRLF&@CRLF, @CRLF)
    $srString = StringReplace($srString, ":", "|")

    Local $aResult2d[0][2]
    _ArrayAdd($aResult2d, $srString)

   _ArrayDisplay($aResult2d)
EndFunc   ;==>_ShowArray

 

Link to comment
Share on other sites

Hi @Fractured :bye:

A little suggestion: don't declare Global variables in Functions, since they are automatically declared as Local.

And, as @mikell said, if you could post a sample string of $sResult variable... :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

2 hours ago, FrancescoDiMuro said:

A little suggestion: don't declare Global variables in Functions, since they are automatically declared as Local.

Hello. Perhaps I'm misunderstanding, but AutoIT has weird behaviour which causes Globals to become permanent variables. Because of this, declaring a Global inside a function will keep it Global. It doesn't become a Local.

E.g.

runFunc()

Func runFunc()
   Global $hello = "Hello"
EndFunc

MsgBox(0, "Test", $hello)

 

Link to comment
Share on other sites

@Fractured
It can be done with StringRegExpReplace() either :)

#include <Array.au3>

Global $strString = "route: 0100" & @CRLF & _
                    "S1: 1" & @CRLF & _
                    @CRLF & _
                    @CRLF & _
                    "J13P1 out (HF_AMP): 1", _
       $arrStrings, _
       $arr2DArray[1][2] = [["Property", "Value"]]

$arrStrings = StringSplit($strString, @CRLF, $STR_ENTIRESPLIT)

For $i = 1 To $arrStrings[0] Step 1
    If $arrStrings[$i] <> "" Then _ArrayAdd($arr2DArray, StringRegExpReplace($arrStrings[$i], "^([^:]+)(?::\s?)(.+)$", "$1|$2"))
Next

_ArrayDisplay($arr2DArray)

Hope @mikell will enjoy this one :D

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Nice! So since I have the Global array "experts" (hehe) here...the script pulls the initial switch settings for the unit. After that, during test, switches will be changed as needed. So to update the array to the new switch positions, would I just reassign that "cell", i.e.  If I change S1 to position 2 , would it work like this?   ---->     $aResult2d[1][1] = 2

switcharray.png

Link to comment
Share on other sites

@Fractured
When you say that you'd like to change your switches as needed, what do you mean?
If you want to change the value of a particular switch, then you can acccess the value with $aResult2d[Row][Column (always 1 in your case)] = NewValue :)

EDIT: 
By the way, pay attention to your values... Most of them have spaces.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

The script is part of a larger program that is used to test electronic black boxes. It communicates through the serial connection with the box to control the switches, attenuators, etc inside the black boxes. The program has a function that monitors the various components, and displays the changes. i.e. if I tell the box to change switch 1 to position 2, the program will update a "button light" to show the new position in the box. So as the the tester goes through, switching things from their initial settings, I need the script to be able to keep track of those positions and update the array as needed.

exampletest.png

Link to comment
Share on other sites

9 minutes ago, Fractured said:

So to update the array to the new switch positions, would I just reassign that "cell"

Yes. Please read all about "arrays" in the help file  :)

25 minutes ago, FrancescoDiMuro said:

Hope @mikell will enjoy this one

:D
But I am not fond of _ArrayAdd's in a loop. using regex I would rather have done it like this  :closedeyes:

#include <Array.au3>

$str = "route: 0100" & @CRLF & _
                    "S1: 1" & @CRLF & _
                    @CRLF & _
                    @CRLF & _
                    "J13P1 out (HF_AMP): 1"
; Msgbox(0,"", $str)
$res = StringRegExp($str, '(?m)(^.+):(.+$)', 3)
;_ArrayDisplay($res)

Local $res2D[UBound($res)/2][2]
For $i = 0 To UBound($res) - 1
    $res2D[$i/2][Mod($i, 2)] = $res[$i]
Next
_ArrayDisplay($res2D)

 

Link to comment
Share on other sites

The "Show Stat" button is to manually query the box to create the initial switch settings to the array $aResult2d. Then as the tester uses the "HSS Send" buttons, the switch positions change in the box, and I need to reflect that with the SOT/CAL(J#) "button lights", and update the array to the new switch positions. 

Link to comment
Share on other sites

@mikell, believe me, ive read and re-read the wiki help, the forums, random Google searches, etc!!! Im finding arrays to be quit troublesome. Thats part of the reason I am doing this "little" project, So I can learn how to use them since it seems the more boxes our engineers create, the more there is to keep track of!! I am an electronics guy (20+ years) but this whole programming thing is still quite new to me. Ive learned a ton in the forums and wiki but still tons to learn! Thanks for the help!!!

Link to comment
Share on other sites

why regex if the string is built with two *reliable* delimiters?

#include <Array.au3>

$str = "route: 0100" & @CRLF & _
                    "S1: 1" & @CRLF & _
                    @CRLF & _
                    @CRLF & _
                    "J13P1 out (HF_AMP): 1"

local $aArr[0][2]
_ArrayAdd($aArr , StringStripWS($str , 4) , 0 , ":" , @CR)

_ArrayDisplay($aArr)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

If you need to change values with any frequency, I have to think an ini or map would make those functions a bit easier.  I prefer maps, because you just capture the name of the control that was clicked, then go change the value of the element with that name.

This script must be ran with the beta.

$str = "route: 0100" & @CRLF & _
                    "S1: 1" & @CRLF & _
                    @CRLF & _
                    @CRLF & _
                    "J13P1 out (HF_AMP): 1"


local $map[]
$aArr = stringsplit(stringstripWS($str , 4) , ":" & @CR , 2)

for $i = 0 to ubound($aArr) - 1 step 2
    $map[$aArr[$i]] = $aArr[$i+1]
Next

msgbox(0, '' , $map["route"])
msgbox(0, '' , $map["S1"])
msgbox(0, '' , $map["J13P1 out (HF_AMP)"])

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...