none
Run time variable creating space RRS feed

  • Question

  • 1..10|%{

    Echo 'var'$_=aa

    }

    Gives me output var 1 = aa, var 2 = aa

    I don't want space in var and 1 here I need 'var1' like so on, I am creating random variables, how to remove space


    Tuesday, December 3, 2019 5:20 PM

Answers

  • 1..10|%{
        "var$_=aa"
    }
    

    "Random" has nothing to do with this.  Just use the correct quoting and you will get the correct output.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 3, 2019 5:57 PM
    • Marked as answer by Himanshu_Kulkarni Thursday, December 5, 2019 7:31 AM
    Tuesday, December 3, 2019 5:57 PM

All replies

  • What are you actually trying to do?

    Why do you need to create "random variables" - what does that mean? Do you mean variables with random content, or variable with random names? (If the latter, what is the purpose?)


    -- Bill Stewart [Bill_Stewart]

    Tuesday, December 3, 2019 5:32 PM
    Moderator
  • I am creating key=value pair and further saving in TXT file

    Var1=aa

    Var2=bb like so on, I don't have value problem but key contain space in below code , it stores

    Var 1 = aa

    Var 2 = bb , the space between var and 1 need to remove, I have tried new-variable too but same problem also $_.trim() or trimstart() gives error and won't help at all


    Tuesday, December 3, 2019 5:53 PM
  • 1..10|%{
        "var$_=aa"
    }
    

    "Random" has nothing to do with this.  Just use the correct quoting and you will get the correct output.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 3, 2019 5:57 PM
    • Marked as answer by Himanshu_Kulkarni Thursday, December 5, 2019 7:31 AM
    Tuesday, December 3, 2019 5:57 PM
  • $hash = @{}
    1..10|%{
        $hash.Add("var$_",'aa')
    }
    $hash
    


    \_(ツ)_/

    Tuesday, December 3, 2019 6:00 PM
  • The question and the followup information don't make sense; sorry. I suspect a language barrier.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, December 3, 2019 6:34 PM
    Moderator
  • use 

    1..10|%{ New-Variable -Name "var$_" -value $_}

    Get-Variable Var*

    Tuesday, December 3, 2019 6:42 PM