Sign in to follow this  
Followers 0
erifash

Two-dimensional array problems!

6 posts in this topic

I have been trying to make my own version of SolidSnake's mounting functions but have run into problems with _MountList(). I have tested my version of _MountList() with one drive subst'ed. I know that the function is creating the two dimensional array correctly and, in theory, the code should work perfectly. I have commented the code to help better understand what is happening. Even if variable $n_values returns an error, it should keep on working regardless. Someone help! :)

Func _MountList()
    Local $sz_drvlst = StringTrimRight(_RetCmd('SUBST'), 2)                 ;trim off the two @CRLFs at the end of the drive list
    If StringStripWS($sz_drvlst, 8) = '' Then Return ''                 ;if no drives are subst'ed the return a blank string
    Local $n_values = StringSplit($sz_drvlst, @CRLF, 1), $n_err = @ERROR            ;get how many "values" (lines) are in the drive list
    MsgBox(0, "", $n_err & @CRLF & $n_values[0] & @CRLF & $n_values[1])         ;error-checking
    Local $a_drvlst[$n_values[0] + 1][2], $a_drvlst[0][0] = $n_values[0], $i, $sz_subdrvltrpth;Create the two-dimensional array (the error is here)
    For $i = 1 to $n_values[0]                              ;initiate a for...next loop
        $sz_subdrvltrpth = StringSplit($n_values[$i], '\: => ', 1)          ;split the drive from the folder on the $i line of the drive list
        $a_drvlst[$i][0] = $sz_subdrvltrpth[1]                      ;store the drive letter to the array
        $a_drvlst[$i][1] = $sz_subdrvltrpth[2]                      ;store the path to the array
    Next                                            ;end for...next loop
    Return $a_drvlst                                    ;return the two-dimensional array
EndFunc

Func _RetCmd($sz_cmdline)
    Local $sz_autotmp = _CreateTemp()
    RunWait(@ComSpec & ' /c ' & $sz_cmdline & ' > ' & $sz_autotmp, @SystemDir, @SW_HIDE)
    Local $n_error = @ERROR, $sz_cmdcontents = _ReadFile($sz_autotmp)
    FileDelete($sz_autotmp)
    SetError($n_error)
    Return $sz_cmdcontents
EndFunc

Func _CreateTemp()
    Local $sz_tmpex
    Do
        $sz_tmpex = @TempDir & "\t" & Int(Random(0, 99999)) & ".tmp"
    Until not FileExists($sz_tmpex)
    Return $sz_tmpex
EndFunc

Func _ReadFile($sz_filepath)
    If FileExists($sz_filepath) Then Return FileRead($sz_filepath, FileGetSize($sz_filepath))
    SetError(1)
    Return 0
EndFunc

P.S. It helps to read the comments if you edit the script in notepad

Share this post


Link to post
Share on other sites



Local $a_drvlst[$n_values[0] + 1][2], $a_drvlst[0][0] = $n_values[0], $i, $sz_subdrvltrpth;Create the two-dimensional array (the error is here)

<{POST_SNAPBACK}>

1) You can't create an array of 0 elements. Try $a_drvlst[1][1] instead.

2) You can't set the value of an array while creating it. You have to do it in a seperate line.

Share this post


Link to post
Share on other sites

You mean like this?

Func _MountList()
    Local $sz_drvlst = StringTrimRight(_RetCmd('SUBST'), 2)     ;trim off the two @CRLFs at the end of the drive list
    If StringStripWS($sz_drvlst, 8) = '' Then Return ''     ;if no drives are subst'ed the return a blank string
    Local $n_values = StringSplit($sz_drvlst, @CRLF, 1), $n_err = @ERROR;get how many "values" (lines) are in the drive list
    MsgBox(0, "", $n_err & @CRLF & $n_values[0] & @CRLF & $n_values[1]);error-checking
    Dim $a_drvlst[$n_values[0] + 1][2]              ;Create the two-dimensional array
    Local $a_drvlst[0][0] = $n_values[0], $i, $sz_subdrvltrpth  ;add data to the array (the error is here)
    For $i = 1 to $n_values[0]                  ;initiate a for...next loop
        $sz_subdrvltrpth = StringSplit($n_values[$i], '\: => ', 1);split the drive from the folder on the $i line of the drive list
        $a_drvlst[$i][0] = $sz_subdrvltrpth[1]          ;store the drive letter to the array
        $a_drvlst[$i][1] = $sz_subdrvltrpth[2]          ;store the path to the array
    Next                                ;end for...next loop
    Return $a_drvlst                        ;return the two-dimensional array
EndFunc

...But I still get an error when trying to add the number of lines in the drive list to $a_drvlst[0][0] even though I created it correctly.

Share this post


Link to post
Share on other sites

You mean like this?

Local $a_drvlst[0][0] = $n_values[0]

...But I still get an error when trying to add the number of lines in the drive list to $a_drvlst[0][0] even though I created it correctly.

<{POST_SNAPBACK}>

Same problem you had before: You cannot declare an array to be [0][0] in size, nor can you set it equal to another array while creating it

Share this post


Link to post
Share on other sites

1) You can't create an array of 0 elements.  Try $a_drvlst[1][1] instead.

2) You can't set the value of an array while creating it.  You have to do it in a seperate line.

<{POST_SNAPBACK}>

1.) I am not creating a zero element array as you can see in the code here:

Dim $a_drvlst[$n_values[0] + 1][2]

2.) I fixed that and it still doesn't work.

Can show me how to fix this (as in code) please? I understand what you are telling me but when I try and fix it, it won't work.

Share this post


Link to post
Share on other sites

replace this:

Dim $a_drvlst[$n_values[0] + 1][2]             ;Create the two-dimensional array
    Local $a_drvlst[0][0] = $n_values[0], $i, $sz_subdrvltrpth   ;add data to the array (the error is here)

with this:

Local $a_drvlst[$n_values[0] + 1][2], $i, $sz_subdrvltrpth
    $a_drvlst[0][0] = $n_values[0]

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