Jump to content
Jemboy

Data records with Array

Recommended Posts

Jemboy

Hi, 

About 25 years ago I used to use (Turbo) Pascal about the sameway I use AutoIt now. Building little tools to make life easier.
I remember, Turbo Pascal had data records, you could use with arrays and if memory serve me, you could even save en read the records to/from a file.

See example at: http://pascal-programming.info/lesson11.php

A Record is a datastructure with some variables in it, making it easy to manipulate.
You could send or return a record to/from a function, so you don't have to work with global variables when you have to return multiple variabeles.

You could read a bunch of recordsets as an array and just use MyArray [1].Name and MyArray[1].Surname,
instead of MyArray[1][0] and MyArray[1][1] etc. making the source easier to read.

 

My question, is something like records implemented into Autoit, is there somekind of UDF that will do the trick or
is these feature somehow planned for the future?

Regards,

Jem.

P.s. sorry for using the link, but I started explaining this record thing and found that the link eplainded it better than I could ever do!

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
Melba23

Jemboy,

You could look at the experimental Map datastructure - this thread explains all that is publicly available about them.

Personally I still tend to use arrays and Enums to make the code easier to follow:

#include <MsgBoxConstants.au3>

Enum $eName, $eSurname ; Set to 0, 1



Global $aNames[2][2] = [["Fred", "Bloggs"], ["Dora", "Smith"]]

For $i = 0 To 1
    MsgBox($MB_SYSTEMMODAL, "First", $aNames[$i][$eName] & " " & $aNames[$i][$eSurname])
Next

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
jvanegmond

AutoIt beta added a Maps feature which is an excellent drop in replacement for this. It's not identical, you won't have any compile time checking done on the names used, but it's definitely a handy thing to have and may solve your use case.

Dim $myBookRec[]

$myBookRec.Title  = 'Some Book'
$myBookRec.Author = 'Victor John Saliba'
$myBookRec.ISBN   = '0-12-345678-9'
$myBookRec.Price  = 25.5

ConsoleWrite('Here are the book details:' & @CRLF)
ConsoleWrite(@CRLF)
ConsoleWrite('Title:  ' & $myBookRec.Title & @CRLF)
ConsoleWrite('Author: ' & $myBookRec.Author & @CRLF)
ConsoleWrite('ISBN:   ' & $myBookRec.ISBN & @CRLF)
ConsoleWrite('Price:  ' & $myBookRec.Price & @CRLF)
  • Like 1

Share this post


Link to post
Share on other sites
jchd

The nearest AutoIt construct is DllStruct but note that you have to declare each of them explicitely if you want to make an array of them.

Maps aren't close to Pascal records.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jchd

A dev??? What do you mean?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
RTFC

@jemboy: you can stick a struct directly into an array, and then access specific fields within the struct, for any desired record within scope, with DllstructGetData/SetData:

Local Const $tagSTRUCT1 = "struct;int var1;byte var2;uint var3;char var4[128];endstruct"
$maxrecords=10

; create data struct for each record
Global $records[$maxrecords+1]
For $rc=1 To $maxrecords
    $records[$rc] = DllStructCreate($tagSTRUCT1)
Next
$records[0]=Ubound($records)-1

; load some content
DllStructSetData($records[1],"var4","lastname1")
DllStructSetData($records[2],"var4","lastname2")
DllStructSetData($records[3],"var4","lastname3")

; retrieve a field for a specific record
$myrecord=2
MsgBox(0,"struct in rec " & $myrecord,DllStructGetData($records[$myrecord],"var4"))

 

Edited by RTFC
  • Like 1

Share this post


Link to post
Share on other sites
Melba23

RTFC,

While I appreciate that you can use Structs to store data, this has always been strongly opposed by the Devs on the grounds that the internals of Structs are not documented other than as a way to pass parameters when calling a DLL.  I kept this quote from Jon when the subject was raised a while ago:

The big worry - and something Valik pointed out in priv-devchat - is that without doubt people will [..] create some hybrid datatypes that are vaguely documented and unsupported. They'll be used with other features in unsupported ways and then we get trapped when we try and change some behaviour that was undefined anyway. They'll definately be used outside of the scope of just being passed to DllCall.

So I would absolutely NOT recommend using them in this way - and certainly not expecting any form of support, or sympathy if they suddenly no longer work as expected.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
RTFC

@M23:Sorry, I was unaware of this discussion and that the approach I suggested was frowned upon. However, using structs themselves as containers for multiple fields of different type is documented, and they offer quick access to individual members. I personally use them in various languages for storing associated bits of data; I wasn't aware I was falling foul of canonical use of AutoIt in this.

Edited by RTFC
typo
  • Like 1

Share this post


Link to post
Share on other sites
jchd

If DllStruct stop working the way they work today there will be more than unexpected behaviors. For instance would _WinAPI_WriteFile stop working when it's passed a DllStruct, which is readily used in some standard includes and Wrapper, to say the least? That is exactly what the OP could be using DllStruct for. Also would the large number of DllStructs used in other WinAPI calls stop working?

For me DllStructs are containers which can hold data under a strict format definable by users with well documented basic types. Maybe both Valik and Jon feared completely different [mis]uses, but certainly not this one.

  • Like 2

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
RTFC

@jchd: thanks. I was sort of hoping/expecting you'd see it that way too.:) But I think Melba's point in removing my post was more specifically aimed at the example script I posted, which could be/was apparently construed as subverting the proper AutoIt way of defining a single struct in a stand-alone variable for Dll parsing, and thereby (inadvertently) advertising a type of usage that the devs would rather discourage. Your broader definition certainly fits my own coding style, though. But I don't know enough about AutoIt's internal workings to be able to judge whether the devs' concerns are legitimate here. Let's just leave it at that. After all, I was only trying to help the OP, and such a fundamental, universal data container as a struct seemed a more obvious suggestion (to me) than a beta feature.:(

Edited by RTFC
  • Like 1

Share this post


Link to post
Share on other sites
Melba23

RTFC,

Melba's point in removing my post

Just to be clear, I have not removed anything at all in this thread - all I did was highlight the opinion of the language author, which I thought might be of interest to any reading.

  I don't know enough about AutoIt's internal workings to be able to judge whether the devs' concerns are legitimate here

However the Devs do know and they discourage such usage - that is good enough for me. But I said above, there is nothing to stop you doing whatever you want - just expect neither support nor sympathy if there is an internal change which still allows Structs to be used for calling DLLs but breaks any code you have previously written.

M23

Edit: Another (ex)Dev who also thinks it is a bad idea: 

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
jchd

The main points remains that the OP asked for an AutoIt construct similar in essential properties to the Pascal record and this does have an answer: DllStruct are just doing that, keeping in mind that they are an equivalent to C structs but not to C typedefed structs.

What DllStructs are not suitable for is when one expects to use them for, say, masquerading the inner layout of objects or similar acrobatic tricks. This is the kind of sideways use that I believe Valik and Jon had in mind. That or doing tricks with the stack or even more dangerous misuse. Agreed, AutoIt isn't the language of choice to rewrite Stuxnet.

I'd like to see a rebutal to the fact that they can be used (and are used today) to pass data to _WinAPI_WriteFile and from _WinAPI_ReadFile as an opaque binary container (address and size). This is all the OP wanted and I see no reason why such legitimate use should be discouraged.

Yet again I must warn against the fact that, contrary to Pascal records, assigning an existing DllStruct to another variable doesn't create a distinct copy of the DllStruct, but a clone of the original struct based at the same address.

Local $tBuf = DllStructCreate('dword')
DllStructSetData($tBuf, 1, 1234)
ConsoleWrite(DllStructGetData($tBuf, 1) & @LF)
Local $tBufCopy = $tBuf
DllStructSetData($tBuf, 1, 5678)
ConsoleWrite(DllStructGetData($tBufCopy, 1) & @LF)

 

  • Like 2

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
LarsJ

I think the OP asked for something like this:

Local $tagMyRecord = "int Integer;double Real;char String[128]"
Local $aMyArray[3] = [ DllStructCreate( $tagMyRecord ), _
                       DllStructCreate( $tagMyRecord ), _
                       DllStructCreate( $tagMyRecord ) ]

$aMyArray[0].Integer = 1234
$aMyArray[0].Real    = 1234.12
$aMyArray[0].String  = "This is string 0"
$aMyArray[1].Integer = 5678
$aMyArray[1].Real    = 5678.56
$aMyArray[1].String  = "This is string 1"
$aMyArray[2].Integer = 9090
$aMyArray[2].Real    = 9090.90
$aMyArray[2].String  = "This is string 2"

ConsoleWrite( $aMyArray[0].Integer & @LF )
ConsoleWrite( $aMyArray[0].Real    & @LF )
ConsoleWrite( $aMyArray[0].String  & @LF )
ConsoleWrite( $aMyArray[1].Integer & @LF )
ConsoleWrite( $aMyArray[1].Real    & @LF )
ConsoleWrite( $aMyArray[1].String  & @LF )
ConsoleWrite( $aMyArray[2].Integer & @LF )
ConsoleWrite( $aMyArray[2].Real    & @LF )
ConsoleWrite( $aMyArray[2].String  & @LF )

 

  • Like 1

Share this post


Link to post
Share on other sites
RTFC

@Melba:When I was reading this thread yesterday, the post with my example had somehow disappeared, and as this occurred right after you posted your response, I interpreted this (wrongly, as it now turns out) as you having made the decision to remove that entry. Sorry for the confusion. And I take the point about lack of future support, but this basically holds for any part of the language (I remember _Iif() disappearing). But I'm with jchd on structs; I cannot imagine that one would restrict their usage to transient dll I/O receptacles only. Using them as semi-permanent data records in memory would seem fair use to me, and in keeping with all other languages that support them. I also have difficulty imagining how dll-associated use could be kept intact while use as a more permanent storage container would disappear. Whether or not it is wise to stick a struct in an array cell is a far more thorny (and technical) issue, and I'll take it from you that the devs have good reason to discourage this. I was certainly not aware of this. But (at least for my applications) it would be a fairly trivial patch to avoid such usage, if support for it were to be discontinued.

As an aside, I cannot find any mention in the Helpfile that structs should not be used as aggregate data containers in general, but maybe I'm looking in the wrong places.

@jchd: I find struct pointer cloning in the way you describe a highly useful feature, actually; it allows separate UDFs to use their own internal referencing conventions to alter a shared data space, and provided these UDFs are part of the same executing thread, you don't need a mutex..

@LarsJ: interesting notation; never used that.

Edited by RTFC

Share this post


Link to post
Share on other sites
Melba23

RTFC,

No problem - I was just trying to make sure every one had the "bigger picture".  I agree with you that there seems little problem with using Structs in this manner, but that there are some risks associated with so doing as the undocumented internals of AutoIt could change at some time in the future.

I cannot find any mention in the Helpfile that structs should not be used as aggregate data containers

Which I would take as a good hint that they were not intended as such - but that there is no specific prohibition against so doing. And I feel that is a good summary of what this thread has concluded.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Dzenan03
      By Dzenan03
      I want to make a while loop, that creates variables based on a array. For thist I created the array $iDsO with the number and the name of folders in an other folder. Every folder has a different name an I want to create variables(arrays) for each folder that show me all the files in that folder. For example: I have the Folder \Folder1. In it there are the Folders \1, \2, \3. In 1, 2 and 3 there are some files(.png). The array for Folder1 is $iDsO and now I want to crate the arrays $iDsO1, $iDsO2 and $iDsO3 with the files in them can I make something like this:
      While $iDs > 0 ;$iDs is the number of files in Folder1>> $iDsO[0] $iDs#here should come the Foldername for example '1'# = _FileListtoArray(@ProgramFilesDir&"\Folder1\"&$iDsO[$iDs]) $iDs = $iDs - 1 Wend So that in the End I have three variabels ($iDs1, $iDs2 and $iDs3)
       
      Is this posible or if not what could I do instead ( I don´t know the number of folders in Folder1 in the begining).
×