Jump to content

Those arrays again...


erebus
 Share

Recommended Posts

Hello all,

I am using the IniReadSection function to read all the values from an ini file. How should I Dim the array to ensure that if the values don't exist the program will not produce an error? I tried with Dim $val[1][1] (and [2][1], [3][1], [4][1], etc but nothing happened..). I also tried to add some @error checks but with no luck. Any ideas?

While 1
    $msg = GUIGetMsg()
    
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Terminate()
    Case $msg = $load
        $readcombo = GUICtrlRead($combo)
        ReadValues($srv, $readcombo)
        $rar = $var[1][1]; <=== the problem is here - if $var[1][1] and so on does not exist the program stucks..
        $dir = $var[2][1]
        $exe = $var[3][1]
        ExitLoop
    Case $msg = $reg
        $readcombo = GUICtrlRead($combo)
        ReadValues($srv, $readcombo)
        $web = $var[4][1]
        Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe -new " & $web)
    EndSelect
Wend

Func ReadValues($file, $section)
    $var = IniReadSection($file, $section)
    ErrorCheck()
EndFunc

Thank you all in advance,

Link to comment
Share on other sites

Hello all,

I am using the IniReadSection function to read all the values from an ini file. How should I Dim the array to ensure that if the values don't exist the program will not produce an error? I tried with Dim $val[1][1] (and [2][1], [3][1], [4][1], etc but nothing happened..). I also tried to add some @error checks but with no luck. Any ideas?

Staright from Help File:

In addition you can use IsArray to let you know if array exists.

Link to comment
Share on other sites

Hello is8591,

Thank you for the answer. First of all the IniReadSection's @error macro is useless in my case: it is set when there is no data at all; however what I wanted to do was to know if a specific array existed (maybe the first is in place but the third isn't, etc.).

Also I didn't overlook the IsArray function while searching the help file. However it is not clear enough in the documentation that this function has the ability to also check if an array exists, it just says that it checks a variable's type, NOT the existance.

In any case it seems that IsArray is a solution, thanks a lot.

If also anybody can tell me what I did wrong with the use of Dim for the array declaration in the previous example I would be grateful.

Link to comment
Share on other sites

If also anybody can tell me what I did wrong with the use of Dim for the array declaration in the previous example I would be grateful.

Just a theory - never tried it yet:

IniReadSection returns N by 2 array. I don't think that Dim should interfere with Function but I wouldn't Dim variable at all. If I would have to reuse the array for next round I would ReDim.

Besides when you Dim $var[$n][1] this is equivalent to 1 dimension array - proper would be Dim $var[$n][2]

Link to comment
Share on other sites

Ok it seems like IsArray doesn't solve the problem. I tried to use the Dim like this:

Dim $var[4][1]

..but the error remains:

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

Any ideas?

Link to comment
Share on other sites

Even if I Dim [4][100] it does the same when I delete the 4th option from my ini..

Do not Dim the variable - let function do it.

Your errorcheck does nothing because it's inside ReadValues and ReadValues returns without indicating if any errors occured. You have 2 options:

1 - move ReadIniSection in main code and do error checking inline

2 - return status from ReadValues.

Another thing:

insert If IsArray($var) then

;read section values

$rar = $var[1][1];

Else

msgbox(0,"","The section does not exist)

endif

Link to comment
Share on other sites

$rar = $var[1][1]; <=== the problem is here - if $var[1][1] and so on does not exist the program sticks...

I'm aiming way over my head, but is it possible/logical to put "placeholder" values in $var[1][1] if it does not exist? Or would this foul up your code elsewhere? (I'm still working on single dimension arrays...)
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

When you say a section is empty, is the header still there or is the header and the data missing? If the former is true, you could dimension your array based on how many "[" (left brackets) exist in your ini file. If the latter is true, you could dimension based on how many "=" (equal signs) exist in your ini file? Again, just trying to help...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hey jefhal, thanks for the answer. :P

a) The header is there; I check this with IniReadSectionNames.

;) As far as the values are concerned I only need the four first values declared per section. If they exist in the INI file, when using the IniReadSection($file, $section) function the created variables are $var[1][1], $var[2][1], $var[3][1] and $var[4][1]. If there are more values in the INI I don't use them, I just need the first four per section.

c) The problem occurs when there are less than these four values set in the INI. This happens because I am trying to equal one variable with a missing one. The only solution I can see is to check if these vars really exist when running the IniReadSection function or to at least to Dim them at first so as to exist even with no data.

Hope these help more..

Link to comment
Share on other sites

This is paritally pseudo-code:

Local $var = IniReadSection()
If Not @error Then
    Local $max = 4
    If $var[0][0] < 4 Then $max = $var[0][0]
    For $i = 1 To $max
        MsgBox(0, "", $var[$i][1]
    Next
EndIf

First the code calls IniReadSection(). If it doesn't set @error, then we try to use the array we were just given. Since we only want the first 4 items, we set a maximum value of 4. Then to prevent array out-of-bounds we make sure the array contains at least 4 elements. If not, we reset the maximum index to be the number of items in the array. Now we execute a For loop through the array for the maximum number of elements. This code won't cause any array out-of-bounds errors. There should be enough logic demonstrated so that if the code doesn't suit your needs as is, you can see how to make it work like you need.

Link to comment
Share on other sites

The only solution I can see is to check if these vars really exist when running the IniReadSection function or to at least to Dim them at first so as to exist even with no data.

Again, grasping at straws: If you can check to see if they exist, and count how many exist, you can then dim based on the number that are there. Additionally, you might dim all of them to be "4" and then "redim" them to the smaller number if that is the case. Can you share a sample ini file with us? Perhaps one that has 2, 3, 4, 5 values... (I'm such a visual guy...)

Hopefully, I've stalled for time long enough to have a real programmer jump in.... ;)

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

@Valik,

If $var[0][0] < 4 Then ...

That was _exactly_ the line of code I needed! Thank you so much pal, you 've been always there when needed.

@ jefhal,

Hopefully, I've stalled for time long enough to have a real programmer jump in....

I know _exactly_ what you mean, trust me :}

Thanks all of you guys for the great help. ;)

Edited by erebus
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...