Results 1 to 4 of 4

Thread: Having Trouble With an Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006

    Having Trouble With an Array

    Hi all:

    I am having some trouble wrapping my head around this issue I am having; I have a INI file that has sections in it, I need to iterate through the INI file and grab the values from it, and based upon a particular key value I then need to grab its corresponding keys and values. It is probably easier to understand if I post some code and info:

    For example, here is what the text file format will look like...

    [Add Something]
    Status = Use
    Status Group = GroupA
    Status Minimum = 1
    Status Maximum = 6
    Quantity = Do Not Use
    Quantity Group = Amount
    Quantity Minimum = 1
    Quantity Maximum = 5
    Color = Use
    Color Group = Visual
    Color Minimum = 1
    Color Maximum = 8
    Texture = Do Not Use
    Texture Group = Material
    Texture Minimum = 1
    Texture Maximum = 2

    So, I have the following to open the text file and grab all of the key values from the section...

    VB Code:
    1. Sub TesterTester
    2. set ini = CreateObject( "AbyssLabs.INIFile" )
    3. filename = "c:\CalmTest\cc\INIReadConfig.txt"
    4. keys = ini.AllKeys(filename, "Add Something" )

    All the above does is create an object (the object is just an INI Reader and Writer), opens the textfile and then for all keys in the Add Something section store it in an array called keys. This works. (NOTE: the array returned is 1-based)

    Then I loop through keys and split the line so that I just get the value (piece(1)). I then check if the value is "USE"; if it is not "USE" I don't care about the value. If it is USE then I want the first part of the piece(piece(0)) and store it in a Variable (lets call it VariableName). Then I want to loop through the keys again and check to see if the VariableName is in any part of the keys, if it is I need to split it to get the value (piece2(1)). This is working.

    VB Code:
    1. for y = 1 to Ubound(keys)
    2.    piece = Split(keys(y), "=")
    3.    If piece(1) = "Use" then
    4.       VariableName = piece(0)
    5.       for z = 1 to UBound(keys)
    6.           if Instr(keys(z), "" & VariableName & "") > 0 then
    7.           piece2 = Split(keys(z), "=")

    The problem I am having is that I am stuck here; I need to grab the values and store them in an array...then once the inside loop is done I need to pass the values (I should have 4 values) to another function, then continue on looping through the file for the other sections.

    Sorry for a newbie question from a newbie vbscripter! Thanks.

  2. #2
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Rochester, NY

    Re: Having Trouble With an Array

    well.. u could just create a string.. pass that to the other function to be split up?

    strVals = strVals & [NewValue] & ":"


    strVals = Left(strVals,len(strVals)-1) <Remove the last :
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Join Date
    Mar 2006

    Re: Having Trouble With an Array

    I know this is a late reply, but I think I finally figured out what you're trying to achieve (I can be a bit slow on the uptake ):

    VB Code:
    1. Const ForReading = 1
    2. set fso = CreateObject("Scripting.FileSystemObject")
    3. set objFile = fso.OpenTextFile("C:\test.ini", ForReading) 'need the path to your ini file
    4. set re = New RegExp
    5. re.IgnoreCase = True
    6. re.Global = True
    7. re.pattern = "\= use$"
    8. i = 0
    9. groupvar = "arbtext"
    10. Do until objFile.AtEndOfStream
    11.     tempVar = objFile.readline
    12.     set matches = re.execute(tempvar)
    13.     for each match in matches
    14.         splitVar = Split(tempVar, "=")
    15.         groupvar = splitVar(0)
    16.         i = len(groupVar)
    17.     next
    18.     if groupvar = mid(tempVar, 1, i) then
    19.         if not UCase(right(tempVar, 3)) = "USE" then
    20.             outputVal = Split(tempVar, "=")
    21.             msgbox outputVal(0)
    22.             msgbox outputVal(1)
    23.         end if
    25.     end if
    26. Loop

    This will read an ini file (the same as the one you posted) which I dumped in the root of C and called test.ini. It will then output the name and value of each section you want to use.

    It works by firstly reading the line into a variable and then trying to match the pattern = use (not case sensitive). Once it does that, it splits the string and gets the string for the name (e.g. Status) and also the length of the string. It uses this length by looking for that amount of letters at the subsequent line and then trying to match the word (hope that makes sense). The values of all the lines with matches is given by just splitting the string in two using = as the delimiter.

    In this case, I've put in an IF statement so I disregard those lines which seem to be just there to tell the code that we want to USE that section, so lines such as Staus = use would be disregarded. If you want to match them to then just rem out the line: if not UCase(right(tempVar, 3)) = "USE" then and it's corresponding end if line.

    I hope this is what you're after, I could be totally wrong and you're now thinking ***? . It wouldn't be the first time

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2006

    Re: Having Trouble With an Array

    Thank you Static and l12ngo...I appreciate the help! I put this off for a couple of days (I had alot of other work things to do ) but I came up with the following:

    VB Code:
    1. Sub TesterTester
    3. Dim arrINIValues()
    5. set INI = CreateObject( "AbyssLabs.INIFile" )
    6. filename = "c:\CalmTest\cc\INIReadConfig.txt"
    7. keys = INI.AllKeys(filename, "Add Observation" )
    9. For y = 1 to Ubound(keys)
    10.     Redim arrINIValues(1)
    11.     arrSplit = Split(keys(y), "=")
    13.     If arrSplit(1) = "Use" then
    14.        strINIKey = arrSplit(0)
    15.        arrINIValues(0) = strINIKey
    16.        t = 0
    18.        For z = y to UBound(keys)
    19.            If Instr(keys(z), "" & strINIKey & "") > 0 then
    20.               Redim Preserve arrINIValues(UBound(arrINIValues) + 1)
    21.               arrSecondSplit = Split(keys(z), "=")
    22.               t = t + 1
    23.               arrINIValues(t) = arrSecondSplit(1)
    24.            End If
    25.        Next
    27.        'now we can call the function
    28.        call ObservationNameType(arrINIValues(0), arrINIValues(1), arrINIValues(2), arrINIValues(3), arrINIValues(4), arrINIValues(5), arrINIValues(6), 0, Now, 25000, Test, 4)
    30.        'this loop is just for debugging purposes...log.message is part of the  automation tool
    31.        For t = 0 to (UBound(arrINIValues)- 1)
    32.            log.message(arrINIValues(t) & " is in position " & t & " in the array")
    33.        Next        
    35.     End If
    37. Next
    39. Set INI = Nothing
    41. End Sub

    As you can see I have some bad coding styles...I'm working on it!

    Anyway, I may think about implementing your ideas instead but for right now the above code is working, and doesn't seem to be too slow (takes about 1 second to execute the whole code, where the INI file has 138 lines in the section).

    And weren't slow man! It wasn't the listener that was the issue here, it was the communicator I had a hard time thinking what I wanted to do myself.

    Thanks again guys...will probably be back on in a while looking for more help...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width