Jump to content

Array nightmare


Recommended Posts

Hi All

My brain is about to explode, can any of you help me with my following dilemma.

My program reads a file line by line, on each line it has part of a connection string.

Lets say there is currently 3 connection strings, 1 per line (but the amount of connections can change).

My For loop (runs once per line) it makes the connection and grabs the current data. (I now have 3 lots of data one per connection string)

It then waits 1 second and runs again. (I know have 6 bits of data 2 per connection.

My dilemma is storing that data per loop. I need to create an array as follows.

Connection1DataLoop1|Connection2Dataloop1|Connection3Dataloop1

Connection1DataLoop2|Connection2Dataloop2|Connection3Dataloop2

Connection1DataLoop3|Connection2Dataloop3|Connection3Dataloop3

 

 

I have a For loop that runs every second.

It reads a file line by line, on every line it has a name.

 

Link to comment
Share on other sites

Hi All

My brain is about to explode, can any of you help me with my following dilemma.

My program reads a file line by line, on each line it has part of a connection string.

Lets say there is currently 3 connection strings, 1 per line (but the amount of connections can change).

 

My For loop (runs once per line) it makes the connection and grabs the current data. (I now have 3 lots of data one per connection string)

It then waits 1 second and runs again. (I know have 6 bits of data 2 per connection.

My dilemma is storing that data per loop. I need to create an array as follows.

Connection1DataLoop1|Connection2Dataloop1|Connection3Dataloop1

Connection1DataLoop2| Connection2Dataloop2|Connection3Dataloop2

Connection1DataLoop3|Connection2Dataloop3|Connection3Dataloop3

 

Unfortunately I also need it so if the data received from the connection already exists in that Connection(column) it does not get added (duplicate data can exist across columns though) but when the data changes it gets added and fills the gaps in the array. E.G.

Connection1DataLoop1|Connection2Dataloop1|Connection3Dataloop1

Connection1DataLoop3| Connection2Dataloop4|Connection3Dataloop2

Connection1DataLoop4|Connection2Dataloop5|Connection3Dataloop3

 

I hope that makes sense as my brain is mush right now.

 

Hope someone can help me.

Thanks

 

Link to comment
Share on other sites

  • Developers
13 minutes ago, Directlinq said:

This post can be deleted.

Let's not... merged to stick to one topic here. ;)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Here is a simplified version of the code if it helps.

Global $Timer = TimerInit(), $Diff = 0

Local $PathlocationsArray, $ConfigFilePath ="C:\Temp\Config.ini"
_FileReadToArray($ConfigFilePath, $PathlocationsArray, $FRTA_NOCOUNT + $FRTA_ENTIRESPLIT, ",")

  While 1
    $Diff = TimerDiff($Timer)
    If $Diff >= 1000 Then

      For $i = 0 to UBound($PathlocationsArray) -1

         $AllInformationC = $OPCItems.AddItem("S7:[" & $PathlocationsArray [$i][0] & "]DB2090,W0,360",$OPCGroupHandle)
         $AllInformationC.Read(2)
         $AllInformationCArray= $AllInformationC.value


         $AllInformationCArray[7];;This first loop data needs to go into Column 0 Row0 of an array, Second loop data needs to go into Column 1 Row0 of the array. Etc
   
      Next

    $Timer = TimerInit()
    EndIf
    Sleep(10)

   WEnd

Thanks

Edited by Directlinq
Link to comment
Share on other sites

The config file is this.

1955_1
1954_1
1950_1

It uses these to connect to different PLCs and read the current data. In AllinformationCArray is a 1D array of the data received  and I require to store the data located in Row 7.

Hope that helps.

Thanks

Link to comment
Share on other sites

Difficult to test, but can you try:

#include <Array.au3>
#include <File.au3>
Global $Timer = TimerInit(), $Diff = 0

Local $PathlocationsArray, $ConfigFilePath ="C:\Temp\Config.ini"
_FileReadToArray($ConfigFilePath, $PathlocationsArray, $FRTA_NOCOUNT + $FRTA_ENTIRESPLIT, ",")
;~ Create a 2D Array with number of connections as columns
Local $aConnections[0][UBound($PathlocationsArray)]

  While 1
    $Diff = TimerDiff($Timer)
    If $Diff >= 1000 Then
    ReDim $aConnections[UBound($aConnections) + 1][UBound($aConnections, 2)]
      For $i = 0 to UBound($PathlocationsArray) -1
        $AllInformationC = $OPCItems.AddItem("S7:[" & $PathlocationsArray [$i][0] & "]DB2090,W0,360",$OPCGroupHandle)
        $AllInformationC.Read(2)
        $AllInformationCArray= $AllInformationC.value
        If _ArraySearch($aConnections, $PathlocationsArray[$i][0] & $AllInformationCArray[7]) = -1 Then $aConnections[UBound($aConnections) - 1][$i] = $PathlocationsArray[$i][0] & $AllInformationCArray[7]
      Next
    _ArrayDisplay($aConnections)
    $Timer = TimerInit()
    EndIf
    Sleep(10)

   WEnd

 

Link to comment
Share on other sites

You said you didn't want duplicates, the _ArraySearch function checks through the array to see if it exists and does nothing (blank) if it is found.

Remove the following to display all settings.

If _ArraySearch($aConnections, $PathlocationsArray[$i][0] & $AllInformationCArray[7]) = -1 Then

 

Edited by Subz
Link to comment
Share on other sites

Hi you are correct, I don't want duplicates in each column but there can be duplicates between columns.

I also need there not to be any empty spaces in the array as it is a queuing system. So i need to know what is first and forth for example in each column. That is not possible with gaps in the array.

Is that even possible?

Link to comment
Share on other sites

One way is to use an array within the array for example:

#include <Array.au3>
#include <File.au3>
Global $Timer = TimerInit(), $Diff = 0

Local $PathlocationsArray, $ConfigFilePath ="C:\Temp\Config.ini"
_FileReadToArray($ConfigFilePath, $PathlocationsArray, $FRTA_NOCOUNT + $FRTA_ENTIRESPLIT, ",")
;~ Create a 2D Array with number of connections as columns
Local $aConnections[UBound($PathlocationsArray)]
While 1
    $Diff = TimerDiff($Timer)
    If $Diff >= 1000 Then
        For $i = 0 to UBound($PathlocationsArray) - 1
            $AllInformationC = $OPCItems.AddItem("S7:[" & $PathlocationsArray [$i][0] & "]DB2090,W0,360",$OPCGroupHandle)
            $AllInformationC.Read(2)
            $AllInformationCArray= $AllInformationC.value
            If _ArraySearch($aConnections[$i], $PathlocationsArray[$i][0] & $AllInformationCArray[7]) = -1 Then
                If IsArray($aConnections[$i]) Then
                    _ArrayAdd($aConnections[$i], $PathlocationsArray[$i][0] & $AllInformationCArray[7])
                Else
                    Local $aConnection[1] = [$PathlocationsArray[$i][0] & $AllInformationCArray[7]]
                    $aConnections[$i] = $aConnection
                EndIf
            EndIf
        Next
        ;~ Following is just to display the inner arrays
        For $i = 0 To UBound($aConnections) - 1
            If IsArray($aConnections) Then _ArrayDisplay($aConnections[$i])
        Next
        $Timer = TimerInit()
    EndIf
    Sleep(10)
WEnd

 

Link to comment
Share on other sites

That seems to work wonders for the first data connection in the config.ini but I cant see the other 2 inner arrays for the other 2 connections.

There only seems to be data in $AConnections[0]

Thanks

 

Edited by Directlinq
Link to comment
Share on other sites

Hmm can you try this?  As mentioned difficult to test, but the following works for me when I create a Random $AllInformationCArray[7]

#include <Array.au3>
#include <File.au3>
Global $Timer = TimerInit(), $Diff = 0

Local $PathlocationsArray, $ConfigFilePath ="C:\Temp\Config.ini"
_FileReadToArray($ConfigFilePath, $PathlocationsArray, $FRTA_NOCOUNT + $FRTA_ENTIRESPLIT, ",")
;~ Create a 2D Array with number of connections as columns
Local $aConnections[UBound($PathlocationsArray)]
While 1
    $Diff = TimerDiff($Timer)
    If $Diff >= 1000 Then
        For $i = 0 to UBound($PathlocationsArray) - 1
            $AllInformationC = $OPCItems.AddItem("S7:[" & $PathlocationsArray [$i][0] & "]DB2090,W0,360",$OPCGroupHandle)
            $AllInformationC.Read(2)
            $AllInformationCArray= $AllInformationC.value
            If IsArray($aConnections[$i]) Then
                If _ArraySearch($aConnections[$i], $PathlocationsArray[$i][0] & $AllInformationCArray[7]) = -1 Then _ArrayAdd($aConnections[$i], $PathlocationsArray[$i][0] & $AllInformationCArray[7])
            Else
                Local $aConnection[1] = [$PathlocationsArray[$i][0] & $AllInformationCArray[7]]
                $aConnections[$i] = $aConnection
            EndIf
        Next
        ;~ Following is just to display the inner arrays
        For $i = 0 To UBound($aConnections) - 1
            If IsArray($aConnections[$i]) Then _ArrayDisplay($aConnections[$i])
        Next
        $Timer = TimerInit()
    EndIf
    Sleep(10)
WEnd

 

Link to comment
Share on other sites

Excellent This works perfectly.

One last thing, how would i assign one of the values in one of the arrays to a variable?

Normally it would be 

$var = $aConnections[$i][0]

But that does not work in this scenario as it is an array in an array.

Sorry if this is a stupid question.

Thanks

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...