If you’d put this…
$a.id(alpha, beta, gamma)
$a.id(alpha)
$a.id + $Count
… in the PowewrShell_ISE or VSCode, you’d immediately see this is not syntactically correct. All those red underlines show up.
This …
$a.id
… this is a field / keypair from a Csvfile, hashtable, JSON string, etc., with an ‘id’ property that has an array.
So,
You cannot do this, they way you are without modification…
$a.id(alpha, beta, gamma)
To, this…
$a = ('alpha', 'beta', 'gamma')
$a[0]
alpha
$a[1]
beta
$a[2]
gamma
So, if you are saying, this is the way you receive this data, then you are going to have to have whomever / whatever is sending this to you to correct this, or you are going to have to manually / automated restructure this the right way to do what you are after.
If you are saying, you are after the values in the $a.id property which holds single string or an array of values, that is different and it also would not be shown as …
(alpha, beta, gamma)
… it would be, shown in braces not parens…
{alpha, beta, gamma}
So, with that in mind, you are getting this from something, let’s say a file and it’s sending back stuff like this.
$a = @{
'id' = ('alpha', 'beta', 'gamma')
}
$a
Name Value
---- -----
id {alpha, beta, gamma}
Then you can do this.
$a.id.Count
3
$a.id[0]
alpha
$a.id[1]
beta
$a.id[2]
gamma
$a.id[0..2]
alpha
beta
gamma
As for
$a.id(alpha) -> so if i also script $a.id[0] – i get only the 'a'
The reason you are getting the above is the is only one object in the array with x number of characters. So, it’s a simple string element not a array set.
$a1 = @{
'id' = ('alpha')
}
$a1
So, then you have to look at it differently.
$a1.id[0] # give me the first char in the string
a
$a1.id[0..($a.id.Length)] -join '' # get the whole string
alpha
# Or just this, since it is only one entry
$a1.id
alpha
So , you have to evaluate the contents of $a.id every time then make your logic decision regarding just how to handle accessing the content / values.
Meaning…
$a = @{
'id' = ('alpha', 'beta', 'gamma')
}
$a.id.Count
3
$a1 = @{
'id' = ('alpha')
}
$a1.id.Count
1
… and then make your logic decision …
$a.id
alpha
beta
gamma
$a.id[0]
alpha
$a1.id
alpha