Jump to content

Multidimentional Associative array UDF?


Recommended Posts

I did some looking around and I found a lot of codeAssociative arrays (hash/dicts etc). Not many of them that I found seem to support multidimensions except for this one: http://www.autoitscript.com/forum/index.php?showtopic=113182&st=0

This could do what I want, but I think its hard to read and understand the way he sets them up. What I am looking to do is this:

Have a file we will called (wages.csv) that looks like this

Default,0,40,10

Default,41,9999,15

Brian,0,40,15

Brian,41,50,20

Brian,51,9999,22

Chad,0,40,13

Chad,41,9999,17

That would then define a multi-dimentional array like this

Array
(
    [Default] => Array
        (
            [0] => Array
                  (
                      [0] => 0
                      [1] => 40
                      [2] => 10
                   )
            [1] => Array
                  (
                      [0] => 41
                      [1] => 9999
                      [2] => 15
                   )
        )
    [Brian] => Array
        (
            [0] => Array
                  (
                      [0] => 0
                      [1] => 40
                      [2] => 15
                   )
            [1] => Array
                  (
                      [0] => 41
                      [1] => 50
                      [2] => 20
                   )
            [2] => Array
                  (
                      [0] => 51
                      [1] => 9999
                      [2] => 22
                   )
        )
    [Chad] => Array
        (
            [0] => Array
                  (
                      [0] => 0
                      [1] => 40
                      [2] => 13
                   )
            [1] => Array
                  (
                      [0] => 41
                      [1] => 9999
                      [2] => 17
                   )
        )
)

Where default, brian, and chad could be use as keys. The whole point is that it will define a pay range for the employee (the key). If Brian worked 52 hours he would fall under 52-9999 thus getting 22 an hour. etc etc.

Is this the best way approaching this idea or do you have a better idea? If it is...then how can I define the arrays I need to define?

Link to comment
Share on other sites

Simply use a two-dimensional array. That's distinct from arrays in an array!

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)

Link to comment
Share on other sites

Because you want an Associative array, you can fake that.

This works:

Local $tagDefault = 'int[3];'
Enum $Default, $Brian, $Chad, $iCount
Global $a[$iCount]

$a[$Default] = DllStructCreate($tagDefault & $tagDefault)
$a[$Brian]   = DllStructCreate($tagDefault & $tagDefault & $tagDefault)
$a[$Chad]    = DllStructCreate($tagDefault & $tagDefault)

DllStructSetData($a[$Default], 1, 0, 1)
DllStructSetData($a[$Default], 1, 40, 2)
DllStructSetData($a[$Default], 1, 10, 3)
DllStructSetData($a[$Default], 2, 41, 1)
DllStructSetData($a[$Default], 2, 9999, 2)
DllStructSetData($a[$Default], 2, 15, 3)

DllStructSetData($a[$Brian], 1, 0, 1)
DllStructSetData($a[$Brian], 1, 40, 2)
DllStructSetData($a[$Brian], 1, 15, 3)
DllStructSetData($a[$Brian], 2, 41, 1)
DllStructSetData($a[$Brian], 2, 50, 2)
DllStructSetData($a[$Brian], 2, 20, 3)
DllStructSetData($a[$Brian], 3, 51, 1)
DllStructSetData($a[$Brian], 3, 9999, 2)
DllStructSetData($a[$Brian], 3, 22, 3)

DllStructSetData($a[$Chad], 1, 0, 1)
DllStructSetData($a[$Chad], 1, 40, 2)
DllStructSetData($a[$Chad], 1, 13, 3)
DllStructSetData($a[$Chad], 2, 41, 1)
DllStructSetData($a[$Chad], 2, 9999, 2)
DllStructSetData($a[$Chad], 2, 17, 3)




ConsoleWrite(_GetVal($Brian, 22) & @CRLF)
ConsoleWrite(_GetVal($Brian, 43) & @CRLF)
ConsoleWrite(_GetVal($Brian, 57) & @CRLF)
ConsoleWrite(_GetVal($Chad, 35) & @CRLF)
ConsoleWrite(_GetVal($Chad, 45) & @CRLF)



Func _GetVal($Name, $Hour)
    Local $i = 1, $val
    While Not @error
        If Number($Hour) <= DllStructGetData($a[$Name], $i, 2) Then _
            Return DllStructGetData($a[$Name], $i, 3)
        $i += 1
    WEnd
    Return SetError(1,0,-1) ; hour greather than stored max value
EndFunc

Best Regards BugFix  

Link to comment
Share on other sites

Because you want an Associative array, you can fake that.

This works:

Local $tagDefault = 'int[3];'
Enum $Default, $Brian, $Chad, $iCount
Global $a[$iCount]

$a[$Default] = DllStructCreate($tagDefault & $tagDefault)
$a[$Brian]   = DllStructCreate($tagDefault & $tagDefault & $tagDefault)
$a[$Chad]    = DllStructCreate($tagDefault & $tagDefault)

DllStructSetData($a[$Default], 1, 0, 1)
DllStructSetData($a[$Default], 1, 40, 2)
DllStructSetData($a[$Default], 1, 10, 3)
DllStructSetData($a[$Default], 2, 41, 1)
DllStructSetData($a[$Default], 2, 9999, 2)
DllStructSetData($a[$Default], 2, 15, 3)

DllStructSetData($a[$Brian], 1, 0, 1)
DllStructSetData($a[$Brian], 1, 40, 2)
DllStructSetData($a[$Brian], 1, 15, 3)
DllStructSetData($a[$Brian], 2, 41, 1)
DllStructSetData($a[$Brian], 2, 50, 2)
DllStructSetData($a[$Brian], 2, 20, 3)
DllStructSetData($a[$Brian], 3, 51, 1)
DllStructSetData($a[$Brian], 3, 9999, 2)
DllStructSetData($a[$Brian], 3, 22, 3)

DllStructSetData($a[$Chad], 1, 0, 1)
DllStructSetData($a[$Chad], 1, 40, 2)
DllStructSetData($a[$Chad], 1, 13, 3)
DllStructSetData($a[$Chad], 2, 41, 1)
DllStructSetData($a[$Chad], 2, 9999, 2)
DllStructSetData($a[$Chad], 2, 17, 3)




ConsoleWrite(_GetVal($Brian, 22) & @CRLF)
ConsoleWrite(_GetVal($Brian, 43) & @CRLF)
ConsoleWrite(_GetVal($Brian, 57) & @CRLF)
ConsoleWrite(_GetVal($Chad, 35) & @CRLF)
ConsoleWrite(_GetVal($Chad, 45) & @CRLF)



Func _GetVal($Name, $Hour)
    Local $i = 1, $val
    While Not @error
        If Number($Hour) <= DllStructGetData($a[$Name], $i, 2) Then _
            Return DllStructGetData($a[$Name], $i, 3)
        $i += 1
    WEnd
    Return SetError(1,0,-1) ; hour greather than stored max value
EndFunc

This seems to mimic what I need, I want to note the data is pulled from a csv, so it will be dynamic. But I guess I could automate that with a bunch of eval and such.
Link to comment
Share on other sites

  • Moderators

Export the csv to sqlite, and use the _sqlite funcs to get your data.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Export the csv to sqlite, and use the _sqlite funcs to get your data.

I was about to give this advice but got the fear to be called sqlite-maniac.

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)

Link to comment
Share on other sites

  • Moderators

jchd,

got the fear to be called sqlite-maniac

Too late! :blink:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

I would say that, to the contrary, using associative arrays for any serious business-like (payroll or whatever) application is deemed to turn out more complex and spaghetti-prone than using simple SQL.

_SQLite_QuerySingleRow($hDB, "select wage from wages where name like " & _SQLite_Escape($employee) & " and " & $WorkTime & " between lowhours and highhours;", $row)
if @error then
    _SQLite_QuerySingleRow($hDB, "select wage from wages where name like 'default' and " & $WorkTime & " between lowhours and highhours;", $row)
endif
$wage = $row[0]

But, as Melba23 disclosed above, this is just my pet perversion :blink:

Edited by jchd

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)

Link to comment
Share on other sites

  • 2 weeks later...

Sorry for another late reply. So yeah, SQL seems more simple. However, as I stated. the CSV files are redownloaded from a server each time the program is read, so I will still need to use CSV files as a source of data. Which means I will need to convert CSV files to SQLlite databases using AutoIT. Any code on that?

I guess in theory I could have PHP do that work for me, if its easier. And then just download the SQLlight database instead of the CSV file for autoit.

By CSV, I mean exactly that. Comma Separated values(columns), with each row separated by a newline.

Edit: nevermind. I googled "csv to sqlite autoit" ...normally it will search the forums for me... Didnt work for me. I found what I wanted here: http://www.autoitscript.com/forum/index.php?showtopic=117675&st=0&p=819670&hl=sqlite%20import&fromsearch=1&#entry819670

Edit2: There seems to be issues with running command lines using AutoIT...I decided to use this easy to read but ugly code:

Local $hFile = @ScriptDir & "\wages.csv"
    Dim $FileArray

    _SQLite_Startup()
    _SQLite_Open() ; open :memory: Database
    If Not _SQLite_Exec(-1, "CREATE TEMP TABLE wagetable (name, lowhours, highhours, wage);") = $SQLITE_OK Then MsgBox(16, "SQLite Error", _SQLite_ErrMsg())

    _FileReadToArray($hFile, $FileArray)
    _ArrayDelete($FileArray, 0)

    For $Line in $FileArray
        $Values = StringSplit($Line, ",", 2)
        If Not _SQLite_Exec(-1, "INSERT INTO wagetable VALUES ('"&$Values[0]&"','"&$Values[1]&"','"&$Values[2]&"', '"&$Values[3]&"');") = $SQLITE_OK Then MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
    Next
Edited by ParoXsitiC
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...