Jump to content

Logic woes


JohnOne
 Share

Recommended Posts

My limited brains are exhausted trying to figure out how to populate an array from an ini file

My ini looks like this

[Daily}

1-1.99=Wed

1.1-2.02=Thu

1.7-2.77=Fri

2.77-2=Sat

What my aim is, is to get the string before "-" into one dimension of an array, the string after "-" and before "=" into the second dimension of the array and the string after "=" (or the key value) into the third dimension of the array

At the moment I'm getting the 2 dimensional array (key and value)using IniReadSection()

Then looking at it I forsee a massive if, or, and, else etc... loop ahead of me, with stringsplits and array creating, copying, and re creating, with a logic I cant seem to figure out.

My question is, If a StringRegExp pattern of some kind could acheive what I'm after ?

I realize I may be asking a lot, and full expect to be blown out, but I cant even begin to comrehend these patterns, and would like to avoid it for now, if I am heading down the wrong path, and It cannot be done.

Appreciate any replies, and any level of help.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

Is this is what you want:

#include <Array.au3>

$aIniArray = IniReadSection("test.ini", "Daily")

_ArrayDisplay($aIniArray)

Global $aFinalArray[UBound($aIniArray) - 1][3]

For $i = 1 To $aIniArray[0][0]
    $aTemp = StringSplit($aIniArray[$i][0], "-", 2)
    _ArrayDisplay($aTemp)
    $aFinalArray[$i - 1][0] = $aTemp[0]
    $aFinalArray[$i - 1][1] = $aTemp[1]
    $aFinalArray[$i - 1][2] = $aIniArray[$i][1]
Next

_ArrayDisplay($aFinalArray)

Not too complicated, was it? :)

M23

Sorry - Pressed the wrong button too soon!

Edited by Melba23

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

Try using capturing groups.

Local $aDaily = IniReadSection("MyData.ini", "Daily")
Local $aExpanded[UBound($aDaily,1)][3]
Local $sPattern = "^([^-]*)-(.*)$"
Local $I

For $I = 0 To UBound($aDaily, 1)
    $aNums = StringRepExp($sPattern, $aDaily[$I][0], 1)
    $aExpanded[$I][0] = $aNums[0]
    $aExpanded[$I][1] = $aNums[1]
    $aExpanded[$I][2] = $aDaily[$I][1]
Next $I

; Now $aExpanded has the layout you requested.

This is not tested, so there might be bugs in it, but it should at least give you a starting point..

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

JohnOne,

Is this is what you want:

#include <Array.au3>

$aIniArray = IniReadSection("test.ini", "Daily")

_ArrayDisplay($aIniArray)

Global $aFinalArray[UBound($aIniArray) - 1][3]

For $i = 1 To $aIniArray[0][0]
    $aTemp = StringSplit($aIniArray[$i][0], "-", 2)
    _ArrayDisplay($aTemp)
    $aFinalArray[$i - 1][0] = $aTemp[0]
    $aFinalArray[$i - 1][1] = $aTemp[1]
    $aFinalArray[$i - 1][2] = $aIniArray[$i][1]
Next

_ArrayDisplay($aFinalArray)

Not too complicated, was it? ;)

M23

Sorry - Pressed the wrong button too soon!

Holy squirrels in santas socks :)

That does exactly what I was after

I seriously could not see shorter than a 90 something line web of a mess figuring that out, I projected about 1/2 weeks of brain hurt, along with fourty trays of cheap beer.

I greatly appreciate that matey, once again you helped me immensely.

Much Obliged

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Try using capturing groups.

Local $aDaily = IniReadSection("MyData.ini", "Daily")
Local $aExpanded[UBound($aDaily,1)][3]
Local $sPattern = "^([^-]*)-(.*)$"
Local $I

For $I = 0 To UBound($aDaily, 1)
    $aNums = StringRepExp($sPattern, $aDaily[$I][0], 1)
    $aExpanded[$I][0] = $aNums[0]
    $aExpanded[$I][1] = $aNums[1]
    $aExpanded[$I][2] = $aDaily[$I][1]
Next $I

; Now $aExpanded has the layout you requested.

This is not tested, so there might be bugs in it.

Thank you Very Kindly sir

I will test that also

The pattern looks nowhere as complicated as I'd envisioned it, but then again, I imaging the difficult part is dreaming the pattern up.

Thanks again

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Oops, made a mistake. I put the pattern first. The string to test comes first. So it should be.

Also, row 0 of IniReadSection contains state information, not real data, so skip it.

Local $aDaily = IniReadSection("MyData.ini", "Daily")
Local $aExpanded[UBound($aDaily,1)-1][3]
Local $sPattern = "^([^-]*)-(.*)$"
Local $I

For $I = 1 To UBound($aDaily, 1)
    $aNums = StringRepExp($aDaily[$I][0], $sPattern, 1)
    $aExpanded[$I][0] = $aNums[0]
    $aExpanded[$I][1] = $aNums[1]
    $aExpanded[$I][2] = $aDaily[$I][1]
Next $I

; Now $aExpanded has the layout you requested.

This is not tested, so there might be bugs in it, but it should at least give you a starting point.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Try using capturing groups.

Local $aDaily = IniReadSection("MyData.ini", "Daily")
Local $aExpanded[UBound($aDaily,1)][3]
Local $sPattern = "^([^-]*)-(.*)$"
Local $I

For $I = 0 To UBound($aDaily, 1)
    $aNums = StringRepExp($sPattern, $aDaily[$I][0], 1)
    $aExpanded[$I][0] = $aNums[0]
    $aExpanded[$I][0] = $aNums^ ERROR ==> Subscript used with non-Array variable.:
    $aExpanded[$I][1] = $aNums[1]
    $aExpanded[$I][2] = $aDaily[$I][1]
Next $I

; Now $aExpanded has the layout you requested.

This is not tested, so there might be bugs in it, but it should at least give you a starting point..

Just for reference, it did spit out an error.

I hope you dont mind me keeping this for learning purposes, on capturing groups.

Thanks

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Been testing with the code Melba23 so kindly offered, But cannot see what I am doing wrong here.

My msgbox at the end displays "0", when my expectations were for it to display "Fri"

#include <Array.au3>

$aIniArray = IniReadSection("test.ini", "Daily")

_ArrayDisplay($aIniArray)                             ;<===== This Array displays fine

Global $aFinalArray[UBound($aIniArray) - 1][3]

For $i = 1 To $aIniArray[0][0]
    $aTemp = StringSplit($aIniArray[$i][0], "-", 2)
    _ArrayDisplay($aTemp)                         ;<===== This Array displays fine
    $aFinalArray[$i - 1][0] = $aTemp[0]
    $aFinalArray[$i - 1][1] = $aTemp[1]
    $aFinalArray[$i - 1][2] = $aIniArray[$i][1]
Next

_ArrayDisplay($aFinalArray)                           ;<===== This final Array displays fine

;What I have tried to do

Local $vResult = 0
Local $ivar = 0.66
For $i = 0 to UBound($aFinalArray[0][0]) -1
    If $ivar > $aFinalArray[$i][0] And $ivar <= $aFinalArray[$i][1] Then
        $vResult = $aFinalArray[$i][2]
    EndIf
Next

MsgBox(0,"",$vResult)

Ini file

[Daily}

0-0.29=Wed
0.3-0.49=Thu
0.5-0.69=Fri
0.7-0.89=Sat

Can anyone give me a hint as to where I am going wrong ?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

As requested - a hint:

Look at the upper bound of your For...Next loop. ;)

But if you need more:

UBound takes just the array name, not the [0][0] element.

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

Of interest, this will give you the count in the [0][0] element of $aFinalArray - but then you do not need to use UBound:

#include <Array.au3>

$aIniArray = IniReadSection("test.ini", "Daily")

_ArrayDisplay($aIniArray)

Global $aFinalArray[UBound($aIniArray)][3] = [[UBound($aIniArray) - 1]]

_ArrayDisplay($aFinalArray)

For $i = 1 To $aIniArray[0][0]
    $aTemp = StringSplit($aIniArray[$i][0], "-", 2)
    _ArrayDisplay($aTemp)
    $aFinalArray[$i][0] = $aTemp[0]
    $aFinalArray[$i][1] = $aTemp[1]
    $aFinalArray[$i][2] = $aIniArray[$i][1]
Next

_ArrayDisplay($aFinalArray)

Local $vResult = 0
Local $ivar = 0.66
For $i = 1 to $aFinalArray[0][0]
    If $ivar > $aFinalArray[$i][0] And $ivar <= $aFinalArray[$i][1] Then
        $vResult = $aFinalArray[$i][2]
    EndIf
Next

MsgBox(0,"",$vResult)

Do not despair - getting array bounds correct is at times difficult. I always have to think carefully and it often takes more than a few practice runs to get it right. :)

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

Just for reference, it did spit out an error.

I hope you dont mind me keeping this for learning purposes, on capturing groups.

Thanks

Please note that I found and fixed an error already. Also, when you report an error, it is best to report the text of the error message and the circumstances it happens under, so that others can get an idea about what your problem really is.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

JohnOne,

As requested - a hint:

Look at the upper bound of your For...Next loop. ;)

But if you need more:

UBound takes just the array name, not the [0][0] element.

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

Of interest, this will give you the count in the [0][0] element of $aFinalArray - but then you do not need to use UBound:

#include <Array.au3>

$aIniArray = IniReadSection("test.ini", "Daily")

_ArrayDisplay($aIniArray)

Global $aFinalArray[UBound($aIniArray)][3] = [[UBound($aIniArray) - 1]]

_ArrayDisplay($aFinalArray)

For $i = 1 To $aIniArray[0][0]
    $aTemp = StringSplit($aIniArray[$i][0], "-", 2)
    _ArrayDisplay($aTemp)
    $aFinalArray[$i][0] = $aTemp[0]
    $aFinalArray[$i][1] = $aTemp[1]
    $aFinalArray[$i][2] = $aIniArray[$i][1]
Next

_ArrayDisplay($aFinalArray)

Local $vResult = 0
Local $ivar = 0.66
For $i = 1 to $aFinalArray[0][0]
    If $ivar > $aFinalArray[$i][0] And $ivar <= $aFinalArray[$i][1] Then
        $vResult = $aFinalArray[$i][2]
    EndIf
Next

MsgBox(0,"",$vResult)

Do not despair - getting array bounds correct is at times difficult. I always have to think carefully and it often takes more than a few practice runs to get it right. :)

M23

Cheers M23, I couldnt see the forest for the trees.

I tried all sorts before looking at your spoiler

I thought the format with [][] made arrays different, not realizing that all dimensions have to be the same size.

Thanks

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Please note that I found and fixed an error already. Also, when you report an error, it is best to report the text of the error message and the circumstances it happens under, so that others can get an idea about what your problem really is.

Thank you

I did reference the error, although I may not have done it correctly

I will try to realize the forum protocol in future, the best I can

Cheers

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

I thought the format with [][] made arrays different, not realizing that all dimensions have to be the same size.

I am somewhat confused by your remark. :) The [][] format (as you call it) determines the number of dimensions in the array (you can have up to 64!) - and nothing says they have to be the same size.

Your problem was that you were passing an element of an array to UBound, rather than the array name. The example I gave you showed how you could use an element as a counter - assuming you had set it, of course. ;)

If you Google "AutoIt wiki array" you will find a good tutorial on arrays - you might find it useful.

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

What I thought was that, all dimensions in an array had to have the same number of elements, for it to be valid (i'm sure I read that in the help files)

I thought that I had to specify a particular dimension though, when defining Ubound

I had it, in the beginning, in my thik skull, that a 3 dimensional array would look like this $array[][][]

<noob admitting noobness smiley>

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

Arrays can have any number of elements in up to 64 dimensions - although 2 is usually as much as a normal brain can cope with (although I have used 3D on a few occasions).

Think of arrays like this. 1D arrays are like lists:

[0] A
[1] B
[2] C
[3] D

2D arrays are like grids:

[0] [1] [2]
[0] A   L   W
[1] B   M   X
[2] C   N   Y
[3] D   O   Z

You can have as many elements as you wish as long as you do not exceed the limit of 16,000,000 - I regularly use an array which has dimensions of [5967][4] (it holds mp3 names, some ID3 info and the date the track was last played). The great thing about arrays is that you can use For...Next loops to fill them and then to access the contents - very useful in many coding situations. Another advantage is that you can pass a single array as a parameter to a function which contains a huge number of variables within it - a very good tip to reduce the need for long parameter lists.

Do look at the tutorial I suggested - arrays are very useful to a coder, but like so much you have to understand them to get the bes tout of them.

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

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