Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scope question - Variable scope within script

37 views
Skip to first unread message

TreyS

unread,
Jan 5, 2010, 11:18:24 AM1/5/10
to
Another newbie question...

I'm hung up on variable scope. Comparing to VbScript, I would like to have
global/public variables visible anywhere within the script, as well as
private/local variables visible only within their own function. As I
understand the docs, global or script scope should be visible anywhere
within a single script.

More specifically, I'm trying to update an global/public array within a
function. The array is visible to the function, and the function adds
members to the array, but on exiting the function, the array reverts to its
state prior to calling the function.

Obviously, I'm missing something here, and its not frustration...

Code sample follows. I can step through this and see the variables
changing. Just don't understand why I lose the modifications made in the
function.

##################################################################################
CLS

$Global:arrAccumulateG = @()
$arrAccumulateG += "Initial entry"
Set-Variable -Name strTest1G -Value 'Intitial value' -Scope Global

Write-Host "Global Variables"
Get-Variable -Scope Global -Name arr*,str*

###################################################################################
Function Accumulate( $StringP )
{
$arrAccumulateG += $StringP
$arrAccumulateG += "Something fixed..."
Get-Variable -Name arrAccumulateG

$strTest1G = $StringP
Write-Host " Added to array:" $arrAccumulateG[ -2..-1 ]
Write-Host " Set value strTest1G: $strTest1G`n"
}
####################################################################################These calls work as expected. (sort of)
#Value is passed in and added to existing, initial, array member.
#However...
# On return from Accumulate function, content of $arrAccumulateG reverts
# to its initial content.
#The initial content is seen in the Accumulate() function, but the
# modification, within the function is lost on return.

Accumulate( "This is line 1." )
Accumulate( "This is line 2." )
Accumulate( "This is line 3." )

Get-Variable -Name arrAccumulateG

Martin Zugec

unread,
Jan 5, 2010, 12:33:54 PM1/5/10
to
Hi They,

in case of global variable, you must say specifically that you want to
modify global variable:
##################################################################################
CLS

$Global:arrAccumulateG = @()
$Global:arrAccumulateG += "Initial entry"


Set-Variable -Name strTest1G -Value 'Intitial value' -Scope Global

Write-Host "Global Variables"
Get-Variable -Scope Global -Name arr*,str*

###################################################################################
Function Accumulate( $StringP )
{

$Global:arrAccumulateG += $StringP
$Global:arrAccumulateG += "Something fixed..."
...

I think you got the idea

Martin

"TreyS" <Tr...@HotMail.com> wrote in message
news:158D8C41-7613-4C41...@microsoft.com...

0 new messages