Sign in to follow this  
Followers 0
litlmike

Storing Array Variables using Embedded Loops

This Question Makes Sense   2 members have voted

  1. 1. Does This Question Makes Sense?

    • Yes
      1
    • No
      0
    • 0

Please sign in or register to vote in this poll.

19 posts in this topic

#1 ·  Posted (edited)

I feel like I am missing the obvious here, but it is just not coming to me. I want to store the data from an .ini into arrays for use later in the script. The .ini is in quotes below the script. I can get the values displayed using MsgBox, but my intuition tells me I am going down the wrong path. I want to store/create an array that 'hosts' all the data from the .ini. Please give me some feedback.

******EDIT****

The script only stores the last loops info.

***************

What I am trying to accomplish long term:

To be able to call on a particular key and extract the value for that key, delimited by '|'. So later on, I will call a function and it will grab key 1, then use the values of key 1 (like 'Test1a' through 'Test4a') to input somewhere else. Is this making sense? My mind is mush, sorry if it doesn't.

Thanks In Advance.

Func FileReader ()
    ;Grab the Data from the .ini
    $Read_Section = IniReadSection ($sFilePath1, "Scheduling")
    ;Seperate the data in .ini into variables
    $Section_Ubound = UBound ($Read_Section) - 1

        For $i = 1 To (UBound ($Read_Section)-1)
            $Keys_Array = StringSplit ( $Read_Section[$i][1], "|" ) ;The Value of Each Key, split into an array
            For $j = 1 To (UBound($Keys_Array)-1) ;Goes through the Value of Key
                MsgBox (0, "", $Keys_Array[$j], 1) ;
            Next
        Next

        Exit

EndFunc

.Ini File looks like this

[scheduling]1=Test1a|Test2a|Test3a|Test4a

2=Test1b|Test2b|Test3b|Test4b

3=Test1c|Test2c|Test3c|Test4c

4=Test1d|Test2d|Test3d|Test4d

Edited by litlmike

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

sorry,didnt read

Edited by Busti

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity

Share this post


Link to post
Share on other sites

There is no clear question.

What are you trying to do that the code you provided does not do?

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

There is no clear question.

What are you trying to do that the code you provided does not do?

Dale

Good question. It has taken me awhile to even figure out how to describe what I am trying to say!

As far as I can tell:

The script only stores the last loops info. Meaning $Keys_Array[1] changes every time the key value changes. I want this function just to store the data, so that other functions can get the data later. Right now, $Keys_Array[1] would only ever show "Test1d", because it is the last loop.

Instead I want to:

Be able to point at key 1 that has 4 values stored uniquely and key 2 the same, and so on. So maybe I need to somehow assign another dimension to the array or something. I really don't know because this is the first time I have attempted to do something in this fashion. This make more sense?

Thanks again!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Let me see if I can confuse you...

#include <Array.au3>

$aReturn = FileReader(@ScriptDir & "\Test.ini")
If Not @error Then
    _ArrayDisplay2D($aReturn)
EndIf


Func FileReader($sFilePath)
    ;Grab the Data from the .ini
    $aSections = IniReadSection($sFilePath, "Scheduling")
    If @error Then Return SetError(1, 0, 0)
    
    Dim $aValues[$aSections[0][0] + 1][4]
    For $i = 1 To $aSections[0][0]
        $aKeys = StringSplit($aSections[$i][1], "|")
        If @error Then ContinueLoop
        ;_ArrayDisplay($aKeys, "Test " & $i)
        For $j = 0 To $aKeys[0] - 1
            $aValues[$i][$j] = $aKeys[$j + 1]
        Next
    Next
    
    Return $aValues
EndFunc   ;==>FileReader

Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) - 1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    For $iCC = $iBase To UBound($aArray, 1) - 1
        For $xCC = 0 To UBound($aArray, 2) - 1
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc   ;==>_ArrayDisplay2D

Edit: Made a couple small changes to the code.

Edited by big_daddy

Share this post


Link to post
Share on other sites

^^ looks familiar.


[center]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.[/center]

Share this post


Link to post
Share on other sites

^^ looks familiar.

Are you referring to the _ArrayDisplay2D()?

Share this post


Link to post
Share on other sites

Are you referring to the _ArrayDisplay2D()?

Actually the Var names and the _ArrayDisplay2D looked familiar...

http://www.autoitscript.com/forum/index.ph...st&p=247041


[center]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.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Let me see if I can confuse you...

Well you succeeded! In both answering the question and confusing me! lol

I have spent a lotta time looking at your code to figure out what the heck it does. I went through and added comments step-by-step to the code. Could you go through and tell me if they are accurate and answer the questions in the comments? On the one hand, I am just happy to have the answer. But, really I am most interested in learning more, so that I can be more helpful to this community, and not so much of a leech! It would be helpful for me to learn from this and increase my coding knowledge.

I think I got a pretty good idea of what you are doing, but there are some holes that I think you could fill for me.

Thanks!

#include <Array.au3>

; I have no idea what this is here for, but it looks cool
$aReturn = FileReader(@ScriptDir & "\Test.ini")
If Not @error Then
    _ArrayDisplay2D($aReturn)
EndIf


Func FileReader($sFilePath)
    ;Grab the Data from the .ini - put into an array with Keys/Values as $aSections[n][m]
    $aSections = IniReadSection($sFilePath, "Scheduling")
    
    ;Manually set the value of the @error macro.
    ;set the @error macro to 1
    ;set the @extended macro to 0
    ;Override the default return value and return this parameter. 
    ;No idea why we should do this
    If @error Then Return SetError(1, 0, 0)
    
    ;Declare that there is a Variable $aValues that is an array and the first dimension is 1 more than the Ubound ([0][0]) of the Read Section Keys
    ;Not sure why 1 more
    Dim $aValues[$aSections[0][0] + 1][4]
    
    ;For $i = 1 through Ubound Ini Read Section
    For $i = 1 To $aSections[0][0]
        ;Split the Keys, delimited by "|" and save into an array $aKeys
        $aKeys = StringSplit($aSections[$i][1], "|")
        ;Not sure why this is here or where it goes after @error
        If @error Then ContinueLoop
;~         ;_ArrayDisplay($aKeys, "Test " & $i)
        
        ;Loop for the Ubound of Keys array
        For $j = 0 To $aKeys[0] - 1
            ;Store the Keys into this array!
            ;Main thing I was missing.
            $aValues[$i][$j] = $aKeys[$j + 1]
        Next
    Next
    
    ;Though sad, after all this time on the forums, I don't know what Return is really used for. :(
    ;Best guess is that it Send $aValues to the Function that Called this Function
    ;Maybe can be used instead of using Global Vars....best guess
    Return $aValues
EndFunc   ;==>FileReader

;Display a 2-D Array 
Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    ;If $aArray is not an array then 'Return' and Set error... Wish I knew that IsArray was a function about 3 weeks ago!
    ;Where does $aArray come from?  Where is it previously declared?
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) - 1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    ;Loop through the First Dimension of $aArray
    For $iCC = $iBase To UBound($aArray, 1) - 1
        ;Loop through the 2nd Dimension of $aArray (up to the Ubound of the 1st dimension - 1)
        For $xCC = 0 To UBound($aArray, 2) - 1
            ;I think the $iCC and $xCC coorelate to the keys, and $aArray must be $aValues?
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    ;If 0 Then Send the Output to the Console?, how would $sToConsole ever equal 0?
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    ;Display Results.  I've always put MsgBox in the Loops, Never thought of doing it this way... nice.
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc   ;==>_ArrayDisplay2D
Edited by litlmike

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

The arraydisplay is only to show you the return. Kind of funny you've used functions before I'm sure, and you don't know where the parameter is coming from.

Just to answer your question, $aArray is declared in the parameter, it's being passed from _ArrayDisplay2D($aReturn), which $aReturned is being declared from FileReader(@ScriptDir & "\Test.ini").

Your only concern should be the FileReader() UDF.

The +1 you have to understand how to use the elements in an array be it single or multi dimensional. [0][0] holds the actual number of elements returned from IniReadSection, but we are declaring an new Array with $aValues thus we need the +1. It could have very well been declared as

Local $aValues[UBound($aSections)][4]

The @error ContinueLoop is because StringSplit() returns an error if none of the delimiters to split the string are found, if it wasn't found, then it would cause the information below it to throw an error.

Return is used to return the value that function produced. This is also nice, because then we don't have to declare every variable Global, but more importantly, we get the specific value returned we need.

The above are really basics from Arrays to Return to error handling. Essential if you want to make smooth running apps.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

The arraydisplay is only to show you the return. Kind of funny you've used functions before

Holy crap! I can't tell you how useful that is...

You are right I have used tons of functions and I don't fully understand how the parameters work. I have used them in some basic scenarios, but never like this. And I have NEVER used Return, even though it is on every Func in the forums! That is sooo helpful.. all those stinking Global variable I have been declaring!!! Omgoodness

I appreciate the error handling too, in that I am just starting to implement error handling into my apps, and I knew from the start I would need them in this script. But, no know-how of how to get there...

Still one question, and now that I think of it, I remember solving an array problem a few weeks back by trail and error and after about an hour decided to +1 and poof! it worked.

[0][0] holds the actual number of elements returned from IniReadSection, but we are declaring an new Array with $aValues thus we need the +1

But why? What about declaring a new array means that we have to +1? Because it is expected to hold more elements than the last array?

Thanks a lot!

Edited by litlmike

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Holy crap! I can't tell you how useful that is...

You are right I have used tons of functions and I don't fully understand how the parameters work. I have used them in some basic scenarios, but never like this. And I have NEVER used Return, even though it is on every Func in the forums! That is sooo helpful.. all those stinking Global variable I have been declaring!!! Omgoodness

I appreciate the error handling too, in that I am just starting to implement error handling into my apps, and I knew from the start I would need them in this script. But, no know-how of how to get there...

Still one question, and now that I think of it, I remember solving an array problem a few weeks back by trail and error and after about an hour decided to +1 and poof! it worked.

But why? What about declaring a new array means that we have to +1? Because it is expected to hold more elements than the last array?

Thanks a lot!

Hmmm... how can I explain this, and not do it wrong or confuse you :)

Ok... Let's try this (Keep in mind, this is only my perspective).

An array is comprised of Elements... Dim $aArray[3] = [Element1,Element2,Element3]

If you look at that example, you see I declared my array with [3] and I have 3 elements.

So, you're probably asking still, why do we add +1... Well let's break it down this way.

$aArray[3] = [0,1,2] << $aArray[0] = Element1 | $aArray[1] = Element2 | $aArray[2] = Element3

The above is a 0 Base return, meaning the actual values returned that we will want to collect will start in [0], I've found this to be a typical return of most other languages I've looked at.

We would use UBound($aArray) - 1 To get the actual number of elements that was declared with a 0 based array, considering [0] doesn't hold our Total element number.

AutoIt, typically but not always returns a 1 base array, however, it returns a 0 base array with StringRegExp(), and it returns a 1 base array with StringSplit(). Meaning that "typically" [0] will hold our Total Elements in the array and not a value from the elements themselves.

Now ... Dim $aArray[4] = [3, Element1,Element2,Element3], although I'm retrieving the same data, there's actually one more element in this array than there is in the 0 base array above. [0] = 3 the total elements, and not Element1 this time.

If we were to do Dim $aArray[3] = [3, Element1,Element2,Element3], we would get a

Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Now because IniReadSection() returns a 1 base Array, meaning it starts at 1 and not 0 for it's values, and [0] contains the total number of elements (minus itself), we must make the exception for that with either using Dim $aValues[uBound($aSections)][4], Where UBound($aSections) contains the proper amount of elements of the array including 0, or $aValues[$aSections[0][0] + 1][4], where $aSections[0][0] contains just the number or Elements found - 0, so we add the additional element needed (+1).

Don't know if that confused you, I haven't proof read it (typing in the fast reply section sucks :D ), and don't know where I rambled, but I'm sure I did... Good Luck.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

Aha! Bravo! That makes perfect sense. Thanks for taking the time to clear that up for me. That makes sense about some arrays have the "Ubound" info in the [0] element and how we must +1 to account for that, because it will show more elements even though there is -1 the number of values.

Share this post


Link to post
Share on other sites

Aha! Bravo! That makes perfect sense. Thanks for taking the time to clear that up for me. That makes sense about some arrays have the "Ubound" info in the [0] element and how we must +1 to account for that, because it will show more elements even though there is -1 the number of values.

Good... Arrays were my stepping stone for me making a world of difference on how I designed my scripts from that point on (once I understand them).

[center]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.[/center]

Share this post


Link to post
Share on other sites

Kind of funny you've used functions before I'm sure, and you don't know where the parameter is coming from.

Question about using Return, does that make a variable now...hmmm... Global? Will I be able to use that variable in other functions? Or does it just return the value of what is being returned? Hope that makes sense, I am seeing an example of how you used Return, I don't comprehend it enough to use in other situations yet. In the example below, does this allow me to use $oExcel in other funcs now?

Thanks

Func Create_Excel ()
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   Create Excel File   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;~  Create an Excel File to Print to
        $oExcel = ObjCreate("Excel.Application")                   ; Create an Excel Object
        $oExcel.Visible = 1                                        ; Let Excel show itself
        $oExcel.WorkBooks.Add   
        Return $oExcel
EndFunc

Share this post


Link to post
Share on other sites

Only if you store the return in a variable.

$oExcel = Create_Excel()

Func Create_Excel ()
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   Create Excel File  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;~  Create an Excel File to Print to
        $oExcel = ObjCreate("Excel.Application")                   ; Create an Excel Object
        $oExcel.Visible = 1                                        ; Let Excel show itself
        $oExcel.WorkBooks.Add   
        Return $oExcel
EndFunc

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
Sign in to follow this  
Followers 0