Windows Vista Forums

array

  1. #1


    MiroslavK Guest

    array

    this works great
    --------------------------------------------
    function Get-RandomElement($Array)
    { $Array[ $Random.Next( $Array.Count ) ] }

    $test="1", "2", "3", "4", "5", "6"

    $Random=New-Object Random

    $Message = Get-RandomElement $test
    $Message
    --------------------------------------------
    but, if I change this line:
    $test="1", "2", "3", "4", "5", "6"
    and put this line:
    $test = Read-Host "input"
    (input is: "1", "2", "3", "4", "5", "6")



    i'm getting single " as result always. whats wrong ?

      My System SpecsSystem Spec

  2. #2


    Gaurhoth Guest

    Re: array

    Read-Host returns a string or secure string... not an array.

    So when $test gets passed an argument for Get-RandomElement, it's trying to get the "count" of a string (which returns nothing) and the Random Object is interpreting that as Zero.

    See this for an example to illustrate:

    $random = new-object Random
    $test = read-host

    INPUT: "1","2","3"

    $random.next($test.count)


    Your function when used with Read-Host is returning the $test[0] character every time (which happens to be " in your example).

    Gaurhoth


    "MiroslavK" <MiroslavK@discussions.microsoft.com> wrote in message newsD9B9064-A84F-4DED-9C47-BB03B06CE559@microsoft.com...
    > this works great
    > --------------------------------------------
    > function Get-RandomElement($Array)
    > { $Array[ $Random.Next( $Array.Count ) ] }
    >
    > $test="1", "2", "3", "4", "5", "6"
    >
    > $Random=New-Object Random
    >
    > $Message = Get-RandomElement $test
    > $Message
    > --------------------------------------------
    > but, if I change this line:
    > $test="1", "2", "3", "4", "5", "6"
    > and put this line:
    > $test = Read-Host "input"
    > (input is: "1", "2", "3", "4", "5", "6")
    >
    > i'm getting single " as result always. whats wrong ?


      My System SpecsSystem Spec

  3. #3


    klumsy@xtra.co.nz Guest

    Re: array

    > $test = Read-Host "input"
    > (input is: "1", "2", "3", "4", "5", "6")
    >
    > i'm getting single " as result always. whats wrong ?



    your input isn't entering an array , but ratehr just one string, and
    thus can't be indexed.. If you are sure that the user is going to type
    the input in the format you wish

    $test = Read-Host "input"
    $test.split(',')
    .....

    however in this case they can just enter in 1,2,3,4,5 since its
    already a string.. if you want them to enter in the "" then you'll
    have to remove that after the .split(',')

    -Karl


      My System SpecsSystem Spec

  4. #4


    hecks@hotmail.co.uk Guest

    Re: array

    On Jul 5, 8:56 pm, "Gaurhoth" <gaurh...@live.com> wrote:
    > Read-Host returns a string or secure string... not an array.
    >
    > So when $test gets passed an argument for Get-RandomElement, it's trying to get the "count" of a string (which returns nothing) and the Random Object is interpreting that as Zero.
    >
    > See this for an example to illustrate:
    >
    > $random = new-object Random
    > $test = read-host
    >
    > INPUT: "1","2","3"
    >
    > $random.next($test.count)
    >
    > Your function when used with Read-Host is returning the $test[0] character every time (which happens to be " in your example).
    >
    > Gaurhoth
    >
    > "MiroslavK" <Mirosl...@discussions.microsoft.com> wrote in messagenewsD9B9064-A84F-4DED-9C47-BB03B06CE559@microsoft.com...
    > > this works great
    > > --------------------------------------------
    > > function Get-RandomElement($Array)
    > > { $Array[ $Random.Next( $Array.Count ) ] }

    >
    > > $test="1", "2", "3", "4", "5", "6"

    >
    > > $Random=New-Object Random

    >
    > > $Message = Get-RandomElement $test
    > > $Message
    > > --------------------------------------------
    > > but, if I change this line:
    > > $test="1", "2", "3", "4", "5", "6"
    > > and put this line:
    > > $test = Read-Host "input"
    > > (input is: "1", "2", "3", "4", "5", "6")

    >
    > > i'm getting single " as result always. whats wrong ?


    This, however, seems to work fine with comma-separated input with or
    without quotation marks:

    function Get-RandomElement($inputstring) {
    $array = invoke-expression $inputstring
    $array[$Random.Next($Array.Count)]
    }

    -Hecks


      My System SpecsSystem Spec

  5. #5


    klumsy@xtra.co.nz Guest

    Re: array


    >
    > function Get-RandomElement($inputstring) {
    > $array = invoke-expression $inputstring
    > $array[$Random.Next($Array.Count)]
    >
    > }
    >
    > -Hecks- Hide quoted text -
    >
    > - Show quoted text -


    and if sometime types del *.* instead of "1","2","3","4" you are in
    trouble


      My System SpecsSystem Spec

  6. #6


    Jacques Barathon [MS] Guest

    Re: array

    <klumsy@xtra.co.nz> wrote in message
    news:1183665488.758290.121450@q69g2000hsb.googlegroups.com...
    ....
    > your input isn't entering an array , but ratehr just one string, and
    > thus can't be indexed..


    I don't think so:

    PS> $string="hello world"
    PS> $string[0]
    h
    PS> $string[10]
    d

    I'd rather think the problem comes from the generation of the random number.
    If you repeatedly create a new random object right before generating a
    random number you will likely generate series of the same number. If instead
    you create a new random object once and then generate new random numbers
    from the same object, you will get a much more effective distribution of
    random results. Don't ask me why, but that is the result of my tests with
    this object.

    Very simple repro test:

    PS> while (1) {$random=new-object random;$random.next(10)}

    Hit Ctrl-C after a couple of seconds and notice the pattern. Now type this:

    PS> $random=new-object random; while (1) {$random.next(10)}

    Hit Ctrl-C again after a couple of seconds. You will notice a distribution
    of numbers much more... random.

    Jacques


      My System SpecsSystem Spec

  7. #7


    klumsy@xtra.co.nz Guest

    Re: array

    i wasn't even looking at the random thing, but at the syntax of
    treating an array of strings that was explicitly declared, versus a
    plain text string input from read-host.


      My System SpecsSystem Spec

  8. #8


    Jacques Barathon [MS] Guest

    Re: array

    <klumsy@xtra.co.nz> wrote in message
    news:1183677365.023926.120860@r34g2000hsd.googlegroups.com...
    >i wasn't even looking at the random thing, but at the syntax of
    > treating an array of strings that was explicitly declared, versus a
    > plain text string input from read-host.


    My bad, sorry. I read the post much too quickly last night. I had noticed
    the misinterpretation of the input from read-host but I hadn't noticed the
    usage of Count which doesn't exist as a method on strings (as explained by
    Gaurhoth in his answer).

    Thanks,
    Jacques


      My System SpecsSystem Spec

array

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
How to create array without quotes? $array = (a,b,c) BenConrad PowerShell 5 04 Feb 2009
Array indexing: Want to say "Item #2 through the rest of the array." Kevin Buchan PowerShell 2 19 Dec 2008
Stupid Array Tricks: Initializing an Array to a Certain Size tojo2000 PowerShell 2 09 Sep 2008
how to assign values to array and how to create array via variable Frank PowerShell 1 13 Mar 2007