![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| Vista 32-bit | How to create array without quotes? $array = (a,b,c) Hi, I'm used to creating arrays in Perl such as: @array = (one, two, three); foreach $a (@array) { print "$a\n"; } In powershell, I can't do that unless I quote each element in the array. I guess that's fine if only have a few elements but If I'm copying and pasting a large list of elements that gets old real quick. I've seen this trick: function new-array {,$args} $array = new-array one,two,three but that seems clunky. Is there a way to create a simple array consisting of comma separated elements without having to quote/doublequote each one? Ben |
My System Specs![]() |
| | #2 (permalink) |
| | RE: How to create array without quotes? $array = (a,b,c) Not the most elegant solution but you can quote the entire set of elements and then use the split() method on them. $array = "a,b,c".split(",") of course that gives you a string array, not sure if that works for you or not. "BenConrad" wrote: Quote: > > Hi, > > I'm used to creating arrays in Perl such as: > > @array = (one, two, three); > foreach $a (@array) { print "$a\n"; } > > In powershell, I can't do that unless I quote each element in the > array. I guess that's fine if only have a few elements but If I'm > copying and pasting a large list of elements that gets old real quick. > > *I've seen this trick*: > > function new-array {,$args} > $array = new-array one,two,three > > but that seems clunky. > > Is there a way to create a simple array consisting of comma separated > elements without having to quote/doublequote each one? > > Ben > > > -- > BenConrad > |
My System Specs![]() |
| | #3 (permalink) |
| Vista 32-bit | Re: How to create array without quotes? $array = (a,b,c) Thanks! That's less clunky than the 'new-array' option: $array = 'one, two twice, three'.split(",") foreach ($a in $array) { $a.Trim() } Not the most elegant solution but you can quote the entire set of elements and then use the split() method on them. $array = "a,b,c".split(",") of course that gives you a string array, not sure if that works for you or not. "BenConrad" wrote: Quote: > > Hi, > > I'm used to creating arrays in Perl such as: > > @array = (one, two, three); > foreach $a (@array) { print "$a\n"; } > > In powershell, I can't do that unless I quote each element in the > array. I guess that's fine if only have a few elements but If I'm > copying and pasting a large list of elements that gets old real quick. > > *I've seen this trick*: > > function new-array {,$args} > $array = new-array one,two,three > > but that seems clunky. > > Is there a way to create a simple array consisting of comma separated > elements without having to quote/doublequote each one? > > Ben > > > -- > BenConrad > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to create array without quotes? $array = (a,b,c) Or you can cheat: function New-Array([Object[]]$Items) { $Items } $Array = New-Array one,two,three Instead of Object[], use a specific type if type coercion matters. Josh "BenConrad" <guest@xxxxxx-email.com> wrote in message news:5b6cd7317f90b2b080ca8c9175d4473b@xxxxxx-gateway.com... Quote: > > Hi, > > I'm used to creating arrays in Perl such as: > > @array = (one, two, three); > foreach $a (@array) { print "$a\n"; } > > In powershell, I can't do that unless I quote each element in the > array. I guess that's fine if only have a few elements but If I'm > copying and pasting a large list of elements that gets old real quick. > > *I've seen this trick*: > > function new-array {,$args} > $array = new-array one,two,three > > but that seems clunky. > > Is there a way to create a simple array consisting of comma separated > elements without having to quote/doublequote each one? > > Ben > > > -- > BenConrad |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to create array without quotes? $array = (a,b,c) Wow... I was really tired when I replied. I didn't even notice the part where you described exactly the same thing. "Josh Einstein" <josheinstein@xxxxxx> wrote in message news:5181B571-E628-481E-A7CF-53915A23C4C0@xxxxxx Quote: > Or you can cheat: > > function New-Array([Object[]]$Items) { > $Items > } > > $Array = New-Array one,two,three > > Instead of Object[], use a specific type if type coercion matters. > > Josh > > "BenConrad" <guest@xxxxxx-email.com> wrote in message > news:5b6cd7317f90b2b080ca8c9175d4473b@xxxxxx-gateway.com... Quote: >> >> Hi, >> >> I'm used to creating arrays in Perl such as: >> >> @array = (one, two, three); >> foreach $a (@array) { print "$a\n"; } >> >> In powershell, I can't do that unless I quote each element in the >> array. I guess that's fine if only have a few elements but If I'm >> copying and pasting a large list of elements that gets old real quick. >> >> *I've seen this trick*: >> >> function new-array {,$args} >> $array = new-array one,two,three >> >> but that seems clunky. >> >> Is there a way to create a simple array consisting of comma separated >> elements without having to quote/doublequote each one? >> >> Ben >> >> >> -- >> BenConrad |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How to create array without quotes? $array = (a,b,c) On Feb 3, 1:57*pm, BenConrad <gu...@xxxxxx-email.com> wrote: Quote: > Hi, > > I'm used to creating arrays in Perl such as: > > @array = (one, two, three); > foreach $a (@array) * *{ * *print "$a\n"; } > > In powershell, I can't do that unless I quote each element in the > array. *I guess that's fine if only have a few elements but If I'm > copying and pasting a large list of elements that gets old real quick. > > *I've seen this trick*: > > function new-array {,$args} > $array = new-array one,two,three > > but that seems clunky. > > Is there a way to create a simple array consisting of comma separated > elements without having to quote/doublequote each one? > > Ben > > -- > BenConrad in Perl. Are you using 'use strict'? The ambiguity of what you're really telling the interpreter can lead to weird and hard to debug side effects. If you wanted to do that just as easily then this would be better: my @array = qw(one two three); |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| create an array of objects? | PowerShell | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| Create a multiline array...how? | VB Script | |||
| how to assign values to array and how to create array via variable | PowerShell | |||