Windows Vista Forums

How to create array without quotes? $array = (a,b,c)
  1. #1



    Newbie
    Join Date : Feb 2009
    Posts : 2
    Vista 32-bit
    Local Time: 12:03 PM

    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

  2. #2


    PaulChavez Guest

    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:

    >
    > 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

  3. #3



    Newbie
    Join Date : Feb 2009
    Posts : 2
    Vista 32-bit
    Local Time: 12:03 PM


      Thread Starter

    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 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:

    >
    > 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

  4. #4


    Josh Einstein Guest

    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...

    >
    > 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

  5. #5


    Josh Einstein Guest

    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

    > 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...

    >>
    >> 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

  6. #6


    tojo2000 Guest

    Re: How to create array without quotes? $array = (a,b,c)

    On Feb 3, 1:57*pm, BenConrad <gu...@xxxxxx-email.com> wrote:

    > 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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast copy method of sub array (=array range) possible? Thomas Lebrecht VB Script 6 19 Mar 2009
Create System.Array Martin Zugec PowerShell 1 19 Sep 2008
Stupid Array Tricks: Initializing an Array to a Certain Size tojo2000 PowerShell 2 09 Sep 2008
Create a multiline array...how? greatbarrier86 VB Script 5 28 May 2008
how to assign values to array and how to create array via variable Frank PowerShell 1 13 Mar 2007