5

I have this in my profile script.

$AddablePaths = @{
    "python3"=";C:\Python32";
    "python2"=";C:\Python27";
    "D"=";C:\D\dmd2\windows\bin";
    "gcc"=";C:\MinGW\bin";
    "verge"=";C:\Users\Cold\Dev\Verge\tools";
    "ruby"=";C:\Ruby192\bin";
    "git"=";C:\Program Files\Git\cmd";
    "cmake"=";C:\Program Files\CMake 2.8\bin";
    "emacs"=";C:\Users\Cold\Dev\emacs\bin";
    "notepad++"=";C:\Program Files\Notepad++";
    "boost-build"=";C:\Users\Cold\Dev\C++\boost-build\bin";
    "svn"=";C:\Program FIles\SlikSvn\bin";
    "gtk2"=";C:\Program Files\GTK2-Runtime\bin";
    "qt"=";C:\Qt\bin";
    "komodo"=";C:\Program Files\ActiveState Komodo Edit 6\";
    "hg"=";C:\Program Files\TortoiseHg\"
}

$AddedPaths = @()

function AddPath($keys)
{
    if ($keys.Count -eq 0) { return }

    foreach ($key in $keys)
    {
        if ($AddablePaths.Contains($key))
        {
            if (!($AddedPaths -contains $key))
            {
                $env:Path += $AddablePaths[$key]
                $AddedPaths += $key
            }
        }
        else
        {
            Write-Host "Invalid path option. Options are:"
            foreach ($key in $AddablePaths.keys) {
                Write "    $key"
            }
        }
    }
}

It's purpose is to allow me to be able to easily add to my path only the things that I need. For example, I can call AddPath("ruby","git","notepad++") to add those three things to my path. I want that it doesn't add items if I already added them, so I created the $AddedPaths array to keep track of what's been added already. However, it doesn't update when I call the function, so duplicates can still be added. What am I doing wrong?

3 Answers 3

6

You'll need to do this:

$Global:AddedPathes += $key

That should be the only place you need $Global: since it modifies it.

Sign up to request clarification or add additional context in comments.

Comments

1

If you make it a hash table instead of an array, you don't have to scope it at all unless you re-use the same variable name in subsequent child scopes.

Comments

1

Since this function is in your profile I think the global scoped variable is probably the best solution. However, I just wanted to let you know there is another way.

In other scenarios (non profile functions, etc), you may want to avoid global scoped variables, but still have a function change a variable and the caller have access to that change. In this case you could create reference variable and pass that to your function (i.e. pass by reference).

You would make the $AddedPaths array of type [ref], then pass it in as a parameter to your function (now with reference type variable):

function AddPath($keys, [ref]$ActivePaths)
{
    if ($keys.Count -eq 0) { return }

    foreach ($key in $keys)
    {
        if ($AddablePaths.Contains($key))
        {
            if (!($ActivePaths.Value -contains $key))
            {
                $env:Path += $AddablePaths[$key]
                $ActivePaths.Value += $key
            }
        }
        else
        {
            Write-Host "Invalid path option. Options are:"
            foreach ($key in $AddablePaths.keys) {
                Write "    $key"
            }
        }
    }
}

> [ref]$AddedPaths = @()
> AddPath -keys ("ruby","git","notepad++") -ActivePaths $AddedPaths

> $AddedPaths

Value
-----
{ruby, git, notepad++}

To get more help on reference variables:

> help about_ref

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.