Jump to content

Scope Concept Question


dufran3
 Share

Recommended Posts

I have a general concept questions. I am using a $count variable to $count my loops. I am searching in a certain location, and anything that contains a string, is added to an array, then $count is incremented for each result it finds...simple...yes I know. However, it is doing this all in the function. Inside the function I have $count as being Global. Here is the function that is incrementing $count

Func SearchReg($searchpath, $valuename)
    $count = 0
    For $i= 1 to 10000
        $currentkey = RegEnumKey($searchpath, $i)
        If @error <> 0 then ExitLoop

        $displayname = RegRead($searchpath & "\" & $currentkey, $valuename)
        
        If StringInStr($displayname,$szRegDisplayName) Then
                    $FileArray[$count]=$displayname
                    $count = $count + 1                 
        EndIf
    Next
    GLOBAL $count
EndFunc

On line 98 I am calling this function above, as you can see, further down, in a For loop, I am using $count, however it is giving me the message that $count hasn't been declared... I thought I understood scope...if you declare Global, you can use variable inside and outside of functions, local, inside or outside only, not both...am I wrong here?

_FileWriteLog($szLogPathFile, 'Searching registry for:' & ' ' & $szRegDisplayName & ' in: ' & $szRegUninstallLocation & $szRegUninstallValue)
SearchReg($szRegUninstallLocation,$szRegUninstallValue)

;----------------------------------;
; Write Array Elements to log File;
;----------------------------------;
    For $i = 0 to $count
        _FileWriteLog($szLogPathFile,'$FileArray[' & $i & '] = ' & $FileArray[$i])
        $i = $i + 1
    Next
Edited by dufran3
Link to comment
Share on other sites

Dim, Local and Global are used to declare scope at the time of variable creation. I do not believe you can use them to CHANGE scope of an existing variable.

Looking briefly at your code, I think what you really want to do if make the value of your $count variable a Return value from your function

Func SearchReg($searchpath, $valuename)
    Local $count = 0
    For $i= 1 to 10000
        $currentkey = RegEnumKey($searchpath, $i)
        If @error <> 0 then ExitLoop

        $displayname = RegRead($searchpath & "\" & $currentkey, $valuename)
        
        If StringInStr($displayname,$szRegDisplayName) Then
                    $FileArray[$count]=$displayname
                    $count = $count + 1                    
        EndIf
    Next
    Return $count
EndFuncoÝ÷ Ù.¶§q©eÊ«~éܶ*')¶¬jëh×6$count = SearchReg($szRegUninstallLocation,$szRegUninstallValue)oÝ÷ Ø­µêòzÙ쨺·îËb¢zÞ¶ê綮¶²¶­Ê­è§µêÞ²×v®¶­sdgVæ26V&6&Vrb33c·6V&6FÂb33c·fÇVVæÖR¢Æö6Âb33c¶6÷VçBÒÂb33c´fÆT'&¢f÷"b33c¶ÒFò¢b33c¶7W'&VçF¶WÒ&VtVçVÔ¶Wb33c·6V&6FÂb33c¶¢bW'&÷"fÇC²fwC²FVâWDÆö÷ ¢b33c¶F7ÆæÖRÒ&Vu&VBb33c·6V&6FfײgV÷C²b3#²gV÷C²fײb33c¶7W'&VçF¶WÂb33c·fÇVVæÖR¢¢b7G&ætå7G"b33c¶F7ÆæÖRÂb33c·7¥&VtF7ÆæÖRFVà¢b33c´fÆT'&²b33c¶6÷VçEÓÒb33c¶F7ÆæÖP¢b33c¶6÷VçBÒb33c¶6÷VçB²¢VæD`¢æW@¢&WGW&âb33c´fÆT'&¤VæDgVæ0oÝ÷ Ù.q©íéÜjv®¶­seôfÆUw&FTÆörb33c·7¤ÆöuFfÆRÂb33µ6V&6ær&Vv7G'f÷#¢b33²fײb33²b33²fײb33c·7¥&VtF7ÆæÖRfײb33²ã¢b33²fײb33c·7¥&VuVæç7FÆÄÆö6Föâfײb33c·7¥&VuVæç7FÆÅfÇVR¢b33c¶×'&Ò6V&6&Vrb33c·7¥&VuVæç7FÆÄÆö6FöâÂb33c·7¥&VuVæç7FÆÅfÇVR £²ÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÓ°£²w&FR'&VÆVÖVçG2FòÆörfÆS°£²ÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÓ°¢f÷"b33c¶ÒFòV&÷VæBb33c¶×'&ÂÒ¢ôfÆUw&FTÆörb33c·7¤ÆöuFfÆRÂb33²b33c¶×'&²b33²fײb33c¶fײb33µÒÒb33²fײb33c¶×'&²b33c¶Ò¢b33c¶Òb33c¶²¢æW

Dale

Edit: broken code tag

Edited by DaleHohm

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

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