Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to create array without quotes? $array = (a,b,c)

Reply
 
Old 02-03-2009   #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 SpecsSystem Spec
Old 02-03-2009   #2 (permalink)
PaulChavez


 
 

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 SpecsSystem Spec
Old 02-03-2009   #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() }


Quote  Quote: Originally Posted by PaulChavez View Post
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 SpecsSystem Spec
Old 02-04-2009   #4 (permalink)
Josh Einstein


 
 

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 SpecsSystem Spec
Old 02-04-2009   #5 (permalink)
Josh Einstein


 
 

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 SpecsSystem Spec
Old 02-04-2009   #6 (permalink)
tojo2000


 
 

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
I don't want to veer too far off topic, but you should never do this
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46