0

So, I have an interesting problem that I'm having no luck solving. I have a need to create a bunch of dynamic array variables.

code:

$temp = 'arr1=(1,2,3);arr2=(4,5,6);arr3=(7,8,9)'
foreach($item in $temp.split(";")){
    $var = $item.split("=")
    New-variable $var[0] $var[1]
    get-variable $var[0]
}

Results:

$arr1 (1,2,3)
...   (4,5,6)
...   (7,8,9)

I have variable $temp which contains a semicolon delimited list of variables/values I want to create. As you can see above I am using new-variable to create this. What is interesting is when I run this from a script, new-variable works for the first call but then I get "..." for all other variables in the array. Any idea what might be causing this?

4
  • $temp = $arr1=(1,2,3);$arr2=(4,5,6) . Is that temp string ? Commented May 21, 2012 at 4:35
  • Your first line doesn't make a semicolon delimited list. It stores the 1,2,3 in $temp and creates $arr2 with 4,5,6 but that's it. Please edit your question to fix the syntax. Commented May 21, 2012 at 5:31
  • It would help to get info what is your scenario, rather than the way you tried to solve it. Looking at the way you did it - you probably doing it wrong.. ;) What exactly is "dynamic" in your data and what end results should be - answering those two question would help others to forge some solution or push you in the right direction... Commented May 21, 2012 at 8:55
  • Sorry, I quickly put this up last night. Thanks for the help all but I figured this one out. I am pulling this string from a config file and when I am splitting the the string I was getting some random character inserted. I fixed this by trimming $var[0]. Commented May 22, 2012 at 0:57

2 Answers 2

2

As Andy already pointed out in a comment, you're not creating a semicolon-separated list, you're creating several variables named $arr1, $arr2, etc. and $temp just refers to $arr1.

A way of fixing your code would be

$temp = 'arr1=(1,2,3);arr2=(4,5,6);...'

Note that for New-Variable you cannot prefix the $ sigil. The variable name is arr1, even if you refer to it in code as $arr1 or (gv arr1).Value or whatever.

But that's a really strange way to write code in PowerShell. You just don't really provide enough detail so that we would actually know what you're doing.

You can just as well do

$arr1 = '(1,2,3)'
$arr2 = '(4,5,6)'
...
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I quickly put this up last night. Thanks for the help all but I figured this one out. I am pulling this string from a config file and when I am splitting the the string I was getting some random character inserted. I fixed this by trimming $var[0].
0

Not sure why this was happening but when I was splitting my string I was getting some random ascii character in my array. Able to see this thanks to PowerGUI!!! The solution was to trim the string prior to using it. (i.e. $var[0].Trim() )

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.