# \[\] cling problem with variablen

**URL:** <https://forums.powershell.org/t/cling-problem-with-variablen/10908>\
**Category:** PowerShell Help\
**Created:** [July 1, 2018, 12:27pm UTC](https://forums.powershell.org/t/cling-problem-with-variablen/10908 "2018-07-01T12:27:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://forums.powershell.org/u/betty)\
**Post date:** [July 1, 2018, 12:27pm UTC](https://forums.powershell.org/t/cling-problem-with-variablen/10908/1 "2018-07-01T12:27:01Z")

</div>

Hello,

i have a problem with a code, and i hope someone can help me.

I have a variable where it is not sure how many entries in it.  
so for exemplare:  
$a.id(alpha, beta, gamma) -\> so i need alpha, i script $a.id[0]

but i don’t no, the variable could also be:  
$a.id(alpha) -\> so if i also script $a.id[0] - i get only the ‘a’

So i try:  
$a.id + $Count -\> but then result is also not the right one -\> $a.id(alpha, beta, gamma, [0])

Is there any solution for the problem?

regards Betty

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [July 1, 2018, 4:35pm UTC](https://forums.powershell.org/t/cling-problem-with-variablen/10908/2 "2018-07-01T16:35:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/cling-problem-with-variablen/10908/3 "2024-05-16T20:34:46Z")

</div>


