0

How i can create array like:

$a -> [1] -> 
             [1] = value1
             [2] = value2
             .................
             [n] = valueN
      [2] -> 
             [1] = value1
             [2] = value2
             .................
             [n] = valueN

and so on. Thank you

i have tried like this:

 $b = @{}

$b[0][0] = 1

$b[0][1] = 2

$b[0][2] = 3

$b[1][0] = 4

$b[1][1] = 5

$b[1][2] = 6

$b

But it doesn't give the required output

2
  • What have you tried so far and what specific issues are you getting? Commented Jul 4, 2019 at 9:20
  • I have tried like this: $b = @{} $b[0][0] = 1 $b[0][1] = 2 $b[0][2] = 3 $b[1][0] = 4 $b[1][1] = 5 $b[1][2] = 6 $b But powershell show me error Cannot index into a null array. Commented Jul 4, 2019 at 9:35

3 Answers 3

1

I think this has been posted multiple times, but simply declare the array and give it values:

[array]$1 = "value1","value2"
[array]$2 = "value1","value2"

[array]$a = $1,$2

$a[0][0] 

will output -> value1 from the first

Please note that declaring the array with [array] is for clarifiying, it is not necessary. If you add comma-seperated values the variable automatically is an array.

EDIT: What you have tried is a hashtable. A hashtable contains a key and a value. An array is only a list of values. A hashtable is created as follows:

 $b = @{
    1 = @{
        1 = "value1"
        2 = "value2"
    }
    2 = @{
        1 = "value1"
        2 = "value2"
    }
    3 = "value3"
 }

$b

As you can see, you can add as many sublevels as you like. To show the value of the first "value1" type:

$b[1].1
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. This is what i need. Have a nice day
How i can dynamicaly create and add to this array values?
You can simply add by doing $b += @{key = "value"}. If this is not exactly what you mean, please post an example of what you are trying to achieve. Sometimes psobjects offer more flexibility.
$k = 0 for($j = 0; $j -le $c.Count; $j+=47) { $b = @{$k = @{} foreach ($cc in $c[$j]) { $b[$k] += @{ 1 = "value1" 2 = "value2" } } $k++ } $b
0

You could use the class approach as well I would prefer:

Add-Type -AssemblyName System.Collections

# Create class with needed members

class myListObject {
    [int]$myIndex
    [System.Collections.Generic.List[string]]$myValue = @()
}

# generic list of class

[System.Collections.Generic.List[myListObject]]$myList = @()

#create and add objects to generic list

$myObject = [myListObject]::new() 
$myObject.myIndex = 1
$myObject.myValue = @( 'value1', 'value2' )

$myList.Add( $myObject )

$myObject = [myListObject]::new() 
$myObject.myIndex = 2
$myObject.myValue = @( 'value3', 'value4' )

$myList.Add( $myObject )


# search items

$myList | Where-Object { $_.myIndex -eq 1 } | Select-Object -Property myValue


$myList | Where-Object { $_.myValue.Contains('value3') } | Select-Object -Property myIndex

Comments

0

The Windows Powershell in Action answer.

$2d = New-Object -TypeName 'object[,]' -ArgumentList 2,2

$2d.Rank
#2

$2d[0,0] = "a"
$2d[1,0] = 'b'
$2d[0,1] = 'c'
$2d[1,1] = 'd'
$2d[1,1]
#d

# slice
$2d[ (0,0) , (1,0) ]
#a
#b

# index variable
$one = 0,0
$two = 1,0
$pair = $one,$two
$2d[ $pair ]
#a
#b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.