![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Get variable data from function to pass back to main for use. Issue trying to solve: I have data in a function where I am reading an xml file and need to pass back up main to use. I started out using an array but if there is a better way please let me know. The program works just need data in availiable in main to store in variables. Please note I did not post xml function of reading data. it works - trying to keep thread some what clean versus tons of script. '******main******* funReadEnvParametersXML strEnvironment 'function to pass in the environment to pull from the xml file. Dim TestArray0 Dim TestArray1 TestArray0 =myarray(0) 'this does work since I am not passing data up. Don't know how to pass array data back up. comes back NULL now. TestArray1 =myarray(1) 'this does work since I am not passing data up. Don't know how to pass array data back up. '------------------------------------------------------function that reads in xml Function funReadEnvParametersXML(strEnvironment) Set xml = GetEnvParametersXML() Set Environments = xml.getElementsByTagName("Environment") For Each Environment In Environments sEnvironmentId=Environment.Attributes.getNamedItem("Id").value Counter = 0 Set variables = Environment.getElementsByTagName("variable") numVariables=variables.length -1 ReDim myarray(numVariables) If (sEnvironmentId = strEnvironment) Then For i = 0 To variables.length -1 sName=variables(i).Attributes.getNamedItem("name").value myarray(i) = sName Next WScript.Echo "Array 0........... " & myarray(0) WScript.Echo "Array 1........... " & myarray(1) '************************ 'I have access to the array results here. How do I pas them back up to the main. '************************ End If Next End Function |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Get variable data from function to pass back to main for use. On Mar 21, 9:44*am, "Todd Vargo" <tlva...@xxxxxx> wrote: Quote: > Big D wrote: Quote: > > Issue trying to solve: I have data in a function where I am reading an xml > > file and need to pass back up main to use. I started out using an array Quote: > > if there is a better way please let me know. The program works just need > > data in availiable in main to store in variables. Please note I did not Quote: > > xml function of reading data. it works - trying to keep thread some what > > clean versus tons of script. Quote: > > '******main******* > You need to declare myarray() before calling the function to make it a > global array. > > Dim myarray() > Quote: > > funReadEnvParametersXML strEnvironment * *'function to pass in the > > environment to pull from the xml file. Quote: > > Dim TestArray0 > > Dim TestArray1 Quote: > > TestArray0 =myarray(0) * *'this does work since I am not passing data up. > > Don't know how to pass array data back up. comes back NULL now. > > TestArray1 =myarray(1) * *'this does work since I am not passing data up. > > Don't know how to pass array data back up. Quote: > > '------------------------------------------------------function that reads > > in xml > > Function funReadEnvParametersXML(strEnvironment) Quote: > > Set xml = GetEnvParametersXML() > > Set Environments = xml.getElementsByTagName("Environment") Quote: > > For Each Environment In Environments > > *sEnvironmentId=Environment.Attributes.getNamedItem("Id").value > > *Counter = 0 > > *Set variables = Environment.getElementsByTagName("variable") Quote: > > *numVariables=variables.length -1 > > *ReDim myarray(numVariables) Quote: > > *If (sEnvironmentId = strEnvironment) Then > > * For i = 0 To variables.length -1 > > * *sName=variables(i).Attributes.getNamedItem("name").value Quote: > > * *myarray(i) = sName > > * Next > > * * * * WScript.Echo "Array 0........... " & myarray(0) > > * * * * WScript.Echo "Array 1........... " & myarray(1) Quote: > > '************************ > > 'I have access to the array results here. How do I pas them back up to the > > main. > > '************************ Quote: > > *End If > > Next Quote: > > End Function Your proposal works, but a better way is to use the Function the way a function is intended to be used. That is, simply equate the array result to the name of the function, as in '************************ 'Function has the array here. It is pass back up to the main, thus ... funReadEnvParametersXML = myarray '************************ Then in the main body, the function is accessed thus ... TestArray = funReadEnvParametersXML(strEnvironment) wsh.echo TestArray(0), TestArray(1) Tom Lavedas ========= |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Get variable data from function to pass back to main for use. Big D wrote: Quote: > Issue trying to solve: I have data in a function where I am reading an xml > file and need to pass back up main to use. I started out using an array Quote: > if there is a better way please let me know. The program works just need > data in availiable in main to store in variables. Please note I did not Quote: > xml function of reading data. it works - trying to keep thread some what > clean versus tons of script. > > '******main******* global array. Dim myarray() Quote: > funReadEnvParametersXML strEnvironment 'function to pass in the > environment to pull from the xml file. > > Dim TestArray0 > Dim TestArray1 > > TestArray0 =myarray(0) 'this does work since I am not passing data up. > Don't know how to pass array data back up. comes back NULL now. > TestArray1 =myarray(1) 'this does work since I am not passing data up. > Don't know how to pass array data back up. > > > > > '------------------------------------------------------function that reads > in xml > Function funReadEnvParametersXML(strEnvironment) > > Set xml = GetEnvParametersXML() > Set Environments = xml.getElementsByTagName("Environment") > > For Each Environment In Environments > sEnvironmentId=Environment.Attributes.getNamedItem("Id").value > Counter = 0 > Set variables = Environment.getElementsByTagName("variable") > > numVariables=variables.length -1 > ReDim myarray(numVariables) > > If (sEnvironmentId = strEnvironment) Then > For i = 0 To variables.length -1 > sName=variables(i).Attributes.getNamedItem("name").value > > myarray(i) = sName > Next > WScript.Echo "Array 0........... " & myarray(0) > WScript.Echo "Array 1........... " & myarray(1) > > '************************ > 'I have access to the array results here. How do I pas them back up to the > main. > '************************ > > > End If > Next > > > > > > End Function > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Get variable data from function to pass back to main for use. Todd Vargo wrote: Quote: > Big D wrote: Quote: > > Issue trying to solve: I have data in a function where I am reading an Quote: Quote: > > file and need to pass back up main to use. I started out using an array Quote: > > if there is a better way please let me know. The program works just need > > data in availiable in main to store in variables. Please note I did not Quote: > > xml function of reading data. it works - trying to keep thread some what > > clean versus tons of script. > > > > '******main******* > You need to declare myarray() before calling the function to make it a > global array. > > Dim myarray() > Quote: > > funReadEnvParametersXML strEnvironment 'function to pass in the > > environment to pull from the xml file. Dim myarr() foo Wscript.Echo myarr(0) & "..." & myarr(1) Function foo() ReDim myarr(2) myarr(0) = "abc" myarr(1) = "xyz" End Function -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Get variable data from function to pass back to main for use. T Lavedas wrote: Todd, Your proposal works, but a better way is to use the Function the way a function is intended to be used. That is, simply equate the array result to the name of the function, as in '************************ 'Function has the array here. It is pass back up to the main, thus ... funReadEnvParametersXML = myarray '************************ Then in the main body, the function is accessed thus ... TestArray = funReadEnvParametersXML(strEnvironment) wsh.echo TestArray(0), TestArray(1) ============================= Tom, Of course, you are right. I was thrown off by the OP's code which led me to believe the data in the array needed to be separated into multiple variables at the main level since they were dimmed separately. Thanks again for your POV. -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Get variable data from function to pass back to main for use. I implemented per your feedback and have it working but ran into a few other concerns of what I was doing...so I slightly changed implementation Removed array and decided to initialize in the function itself by doing the following. I tried using the execute statement and maybe it's not the correct way. My intent was running execute command to set variable. I think I need to intialalise variable in the main but unsure of how to do. Still the same goal: need to set variable in function so I can use it in main. For Each Environment In Environments sEnvironmentId=Environment.Attributes.getNamedItem("Id").value Set variables = Environment.getElementsByTagName("variable") numVariables=variables.length -1 ReDim myArray(numVariables) If (sEnvironmentId = strEnvironment) Then For i = 0 To variables.length -1 sName=variables(i).Attributes.getNamedItem("name").value sValue=variables(i).Attributes.getNamedItem("value").value Execute("str" & sName & "=" & sValue) <--------------------------------Added this line. Get name and value, append str becuase it's a string. The name is unique. need to set to use in main. 'WScript.Echo strStoresADDomain Next End If Next "Todd Vargo" <tlvargo@xxxxxx> wrote in message news:Otp$K5iqJHA.3864@xxxxxx Quote: > Todd Vargo wrote: Quote: >> Big D wrote: Quote: >> > Issue trying to solve: I have data in a function where I am reading an Quote: Quote: >> > file and need to pass back up main to use. I started out using an array Quote: >> > if there is a better way please let me know. The program works just >> > need >> > data in availiable in main to store in variables. Please note I did not Quote: >> > xml function of reading data. it works - trying to keep thread some >> > what >> > clean versus tons of script. >> > >> > '******main******* >> You need to declare myarray() before calling the function to make it a >> global array. >> >> Dim myarray() >> Quote: >> > funReadEnvParametersXML strEnvironment 'function to pass in the >> > environment to pull from the xml file. > This simple example should help... > > Dim myarr() > foo > Wscript.Echo myarr(0) & "..." & myarr(1) > > Function foo() > ReDim myarr(2) > myarr(0) = "abc" > myarr(1) = "xyz" > End Function > > -- > Todd Vargo > (Post questions to group only. Remove "z" to email personal messages) > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Get variable data from function to pass back to main for use. Big D wrote: Quote: > I implemented per your feedback and have it working but ran into a few Quote: > concerns of what I was doing...so I slightly changed implementation > > Removed array and decided to initialize in the function itself by doing Quote: > following. > > I tried using the execute statement and maybe it's not the correct way. My > intent was running execute command to set variable. I think I need to > intialalise variable in the main but unsure of how to do. > > Still the same goal: need to set variable in function so I can use it in > main. > > For Each Environment In Environments > sEnvironmentId=Environment.Attributes.getNamedItem("Id").value > Set variables = Environment.getElementsByTagName("variable") > > numVariables=variables.length -1 > ReDim myArray(numVariables) > > If (sEnvironmentId = strEnvironment) Then > For i = 0 To variables.length -1 > sName=variables(i).Attributes.getNamedItem("name").value > sValue=variables(i).Attributes.getNamedItem("value").value > > Execute("str" & sName & "=" & sValue) > <--------------------------------Added this line. Get name and value, Quote: > str becuase it's a string. The name is unique. need to set to use in main. > 'WScript.Echo strStoresADDomain > Next > End If > Next and then describe the alterations that you want to make to it. -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Using a variable from within a function | PowerShell | |||
| Set a variable in a function then pass it off? | PowerShell | |||
| Why the Value not able to Pass to a Function | PowerShell | |||
| how to pass *properties* to a function | PowerShell | |||
| Howto: pass all args to a sub function? | PowerShell | |||