# Index out of Range

**URL:** <https://forum.inductiveautomation.com/t/index-out-of-range/87639>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [March 20, 2024, 4:09pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639 "2024-03-20T16:09:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 4:09pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/1 "2024-03-20T16:09:10Z")

</div>

Good afternoon all,

I am having an indexing issue due to the way I'm trying to pull data. Here is a snip of my code:

```auto
if field == 5:
	#if all inputs, do this
	if self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "" and self.getSibling("Dropdown").props.value[4] != "":
		days = 11111
	#if there's four outputs, do this
	elif self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "":
		if self.getSibling("Dropdown").props.value[0] == 0 or self.getSibling("Dropdown").props.value[1] == 0 or self.getSibling("Dropdown").props.value[2] == 0 or self.getSibling("Dropdown").props.value[3] == 0:
			Monday = 1
		else:
			Monday = 0
		if self.getSibling("Dropdown").props.value[0] == 1 or self.getSibling("Dropdown").props.value[1] == 1 or self.getSibling("Dropdown").props.value[2] == 1 or self.getSibling("Dropdown").props.value[3] == 1:
			Tuesday = 1
		else:
	Tuesday = 0
		if self.getSibling("Dropdown").props.value[0] == 2 or self.getSibling("Dropdown").props.value[1] == 2 or self.getSibling("Dropdown").props.value[2] == 2 or self.getSibling("Dropdown").props.value[3] == 2:
			Wednesday = 1
		else:
			Wednesday = 0
		if self.getSibling("Dropdown").props.value[0] == 3 or self.getSibling("Dropdown").props.value[1] == 3 or self.getSibling("Dropdown").props.value[2] == 3 or self.getSibling("Dropdown").props.value[3] == 3:
			Thursday = 1
		else:
			Thursday = 0
		if self.getSibling("Dropdown").props.value[0] == 4 or self.getSibling("Dropdown").props.value[1] == 4 or self.getSibling("Dropdown").props.value[2] == 4 or self.getSibling("Dropdown").props.value[3] == 4:
			Friday = 1
		else:
			Friday = 0
		days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)

```

My issue is starting at this line:

```auto
if self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "" and self.getSibling("Dropdown").props.value[4] != "":

```

The issue is that there is not always a list, it could just be one option or it could be all 5 options. When it's not all 5 options and only one option for example, I am unable to run my query as a self.getSibling("Dropdown").props.value[1] may not exist. Does anyone have any tips for situations like this?

PS: I did not set this up this way, I am trying to correct an issue with the programming a CO-OP left us with.

Thank you in advance for reading and participating in this amazing community.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 20, 2024, 4:12pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/2 "2024-03-20T16:12:46Z")

</div>

I ran your code through ChatGPT(4) with specific instructions to optimize, and at a glance the output seems pretty good:

```python
if field == 5:
    values = self.getSibling("Dropdown").props.value
    not_empty_values = all(v != "" for v in values[:5])

    if not_empty_values and len(values) >= 5:
        days = '11111'
    elif not_empty_values and len(values) >= 4:
        day_flags = [any(values[i] == str(day) for i in range(4)) for day in range(5)]
        days = ''.join(['1' if flag else '0' for flag in day_flags])

```

I can't promise this will help with your indexing issue, but it's going to be a hell of a lot easier to maintain.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 4:16pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/3 "2024-03-20T16:16:44Z")

</div>

If that's the case, the whole code is here:

```auto
if field == 5:
	#if all inputs, do this
	if self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "" and self.getSibling("Dropdown").props.value[4] != "":
		days = 11111
	#if there's four outputs, do this
	elif self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "" and self.getSibling("Dropdown").props.value[3] != "":
		if self.getSibling("Dropdown").props.value[0] == 0 or self.getSibling("Dropdown").props.value[1] == 0 or self.getSibling("Dropdown").props.value[2] == 0 or self.getSibling("Dropdown").props.value[3] == 0:
			Monday = 1
		else:
			Monday = 0
		if self.getSibling("Dropdown").props.value[0] == 1 or self.getSibling("Dropdown").props.value[1] == 1 or self.getSibling("Dropdown").props.value[2] == 1 or self.getSibling("Dropdown").props.value[3] == 1:
			Tuesday = 1
		else:
			Tuesday = 0
		if self.getSibling("Dropdown").props.value[0] == 2 or self.getSibling("Dropdown").props.value[1] == 2 or self.getSibling("Dropdown").props.value[2] == 2 or self.getSibling("Dropdown").props.value[3] == 2:
			Wednesday = 1
		else:
			Wednesday = 0
		if self.getSibling("Dropdown").props.value[0] == 3 or self.getSibling("Dropdown").props.value[1] == 3 or self.getSibling("Dropdown").props.value[2] == 3 or self.getSibling("Dropdown").props.value[3] == 3:
			Thursday = 1
		else:
			Thursday = 0
		if self.getSibling("Dropdown").props.value[0] == 4 or self.getSibling("Dropdown").props.value[1] == 4 or self.getSibling("Dropdown").props.value[2] == 4 or self.getSibling("Dropdown").props.value[3] == 4:
			Friday = 1
		else:
			Friday = 0
		days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)
	#if there's three outputs, do this
	elif self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "" and self.getSibling("Dropdown").props.value[2] != "":
		if self.getSibling("Dropdown").props.value[0] == 0 or self.getSibling("Dropdown").props.value[1] == 0 or self.getSibling("Dropdown").props.value[2] == 0:
			Monday = 1
		else:
			Monday = 0
		if self.getSibling("Dropdown").props.value[0] == 1 or self.getSibling("Dropdown").props.value[1] == 1 or self.getSibling("Dropdown").props.value[2] == 1:
			Tuesday = 1
		else:
			Tuesday = 0
		if self.getSibling("Dropdown").props.value[0] == 2 or self.getSibling("Dropdown").props.value[1] == 2 or self.getSibling("Dropdown").props.value[2] == 2:
			Wednesday = 1
		else:
			Wednesday = 0
		if self.getSibling("Dropdown").props.value[0] == 3 or self.getSibling("Dropdown").props.value[1] == 3 or self.getSibling("Dropdown").props.value[2] == 3:
			Thursday = 1
		else:
			Thursday = 0
		if self.getSibling("Dropdown").props.value[0] == 4 or self.getSibling("Dropdown").props.value[1] == 4 or self.getSibling("Dropdown").props.value[2] == 4:
			Friday = 1
		else:
			Friday = 0
		days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)
	#if there's two inputs, do this
	elif self.getSibling("Dropdown").props.value[0] != "" and self.getSibling("Dropdown").props.value[1] != "":
		if self.getSibling("Dropdown").props.value[0] == 0 or self.getSibling("Dropdown").props.value[1] == 0:
			Monday = 1
		else:
			Monday = 0
		if self.getSibling("Dropdown").props.value[0] == 1 or self.getSibling("Dropdown").props.value[1] == 1:
			Tuesday = 1
		else:
			Tuesday = 0
		if self.getSibling("Dropdown").props.value[0] == 2 or self.getSibling("Dropdown").props.value[1] == 2:
			Wednesday = 1
		else:
			Wednesday = 0
		if self.getSibling("Dropdown").props.value[0] == 3 or self.getSibling("Dropdown").props.value[1] == 3:
			Thursday = 1
		else:
			Thursday = 0
		if self.getSibling("Dropdown").props.value[0] == 4 or self.getSibling("Dropdown").props.value[1] == 4:
			Friday = 1
		else:
			Friday = 0
		days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)
	#if there's only one input, do this
	else:
		if self.getSibling("Dropdown").props.value[0] == 0:
			Monday = 1
		else:
			Monday = 0
		if self.getSibling("Dropdown").props.value[0] == 1:
			Tuesday = 1
		else:
			Tuesday = 0
		if self.getSibling("Dropdown").props.value[0] == 2:
			Wednesday = 1
		else:
			Wednesday = 0
		if self.getSibling("Dropdown").props.value[0] == 3:
			Thursday = 1
		else:
			Thursday = 0
		if self.getSibling("Dropdown").props.value[0] == 4:
			Friday = 1
		else:
			Friday = 0
		days = str(Monday) + str(Tuesday) + str(Wednesday) + str(Thursday) + str(Friday)
else:
	days = table.getValueAt(0,6)

```

I've never used chatGPT, are they safe?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 20, 2024, 4:20pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/4 "2024-03-20T16:20:57Z")

</div>

Damn that's a whole forest of [ifs](https://fr.wikipedia.org/wiki/If_commun) !

---

<div class="post-metadata">

**Author:** ![Ryan\_Ahlstrom](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Ryan\_Ahlstrom](https://forum.inductiveautomation.com/u/Ryan_Ahlstrom)\
**Post date:** [March 20, 2024, 4:26pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/5 "2024-03-20T16:26:17Z")

</div>

Hi Robert,  
To get a better idea of how to help, what component is running this script and what is it trying to accomplish? Is the dropdown menu always populated with 5 options (Monday -\> Friday)?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 20, 2024, 4:32pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/6 "2024-03-20T16:32:21Z")

</div>

General comment:

- Create a custom prop of type array on the view. Call it `ddValue`.
- Create a bidirectional property binding from Dropdown's `value` to `view.custom.ddValue`.

Now your code (not that I'm recommending it) becomes:

```auto
if view.custom.ddValue[0] == 0 or view.custom.ddValue[1] == 0 or view.custom.ddValue[2] == 0 orview.custom.ddValue[3] == 0:
    Monday = 1

```

This in turn could be simplified to,

```auto
Monday = view.custom.ddValue[0] == 0 or view.custom.ddValue[1] == 0 or view.custom.ddValue[2] == 0 orview.custom.ddValue[3] == 0
Tuesday = view.custom.ddValue[0] == 1 or view.custom.ddValue[1] == 1 or view.custom.ddValue[2] == 1 or view.custom.ddValue[3] == 1
Wednesday = ...

```

Using the custom property binding gets rid of all the getSiblings which will all break if you rename the component or move it into a container. With the binding to a custom property on the view that won't happen.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 4:35pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/7 "2024-03-20T16:35:36Z")

</div>

Hey Ryan,

A dropdown component is running this script, its purpose is to allow multiple days to be selected, giving a range of "#####" for the weekdays. So if Monday and Tuesday are selected, it will be 110000 whereas all selected will be 11111. I am inputting this into a SQL database.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 4:36pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/8 "2024-03-20T16:36:15Z")

</div>

Lots of possibilities!! Haha

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 4:36pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/9 "2024-03-20T16:36:42Z")

</div>

Thank you Transistor. I probably won't worry too much about this one, should be a fix-up and forget it exists.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 20, 2024, 4:52pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/10 "2024-03-20T16:52:57Z")

</div>

Can you share more detail about how you've got this set up?  
I made a simple multiselect dropdown like this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/0/a0716c306de773a3f949d683d22d38e6b4360dca.png)

And you can achieve the output format you want in essentially two lines of code:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/e/fe4f60cc1db3d8cddf3d7641d81c91b15e77f0cc.png)

```python
def transform(self, value, quality, timestamp):
	if value is None:
		return '00000'
	return ''.join(
        str(int(day in value))
        for day in xrange(1, 6)
    )

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/6/76d982500354723426a0eb359b148f7ae641311e.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 20, 2024, 4:54pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/11 "2024-03-20T16:54:23Z")

</div>

```python
	if field == 5:
		values = self.getSibling("Dropdown").props.value
		days = ['0', '0', '0', '0', '0']
		for i in xrange(len(days)):
			if i in values:
				days[i] = '1'
		days = ''.join(days)
	else:
		days = table.getValueAt(0,6)

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 20, 2024, 4:55pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/12 "2024-03-20T16:55:44Z")

</div>

Use five checkboxes instead.

![Mon-Fri](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/6/e6deb882ec842d4df098ee61d1f1cec407100b6b.png)

Create an expression binding somewhere.

```auto
  if({../chkMon.props.selected}, "1", "0")
+ if({../chkTue.props.selected}, "1", "0")
+ if({../chkWed.props.selected}, "1", "0")
+ if({../chkThu.props.selected}, "1", "0")
+ if({../chkFri.props.selected}, "1", "0")

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 20, 2024, 5:02pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/13 "2024-03-20T17:02:48Z")

</div>

If I understand the logic correctly, I think you could just do this:

```python
days = [
	1 in values,
	2 in values,
	3 in values,
	4 in values,
	5 in values
]
days = ''.join('1' if d else '0' for d in days)

```

But I feel like that's just trying to fix a sinking boat with a bike wheel patch.  
Do what @Transistor suggested instead.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 5:04pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/14 "2024-03-20T17:04:46Z")

</div>

This worked awesome, thank you very much!

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 5:05pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/15 "2024-03-20T17:05:20Z")

</div>

This definitely would've been easier, just don't want to run into any issues elsewhere in his code! Thank you very much.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 20, 2024, 5:10pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/16 "2024-03-20T17:10:26Z")

</div>

Go on. Do the right thing.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 20, 2024, 5:18pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/17 "2024-03-20T17:18:41Z")

</div>

> [@Transistor](#):
>
> Do the right thing.

Agreed. The multiselect on the dropdown is kind of klunky to use, IMO.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [March 20, 2024, 5:28pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/18 "2024-03-20T17:28:14Z")

</div>

Yeah, I have never used it and don't care for it. I just don't want to change the process they're used to.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [March 20, 2024, 5:32pm UTC](https://forum.inductiveautomation.com/t/index-out-of-range/87639/19 "2024-03-20T17:32:28Z")

</div>

> [@Robert\_Hrynciw](#):
>
> chatGPT, are they safe?

Think of ChatGPT as a clever but error-prone co-op, you have to carefully check its code for mistakes before you trust production with it. Sometimes the stuff it comes up with is just plain weird, too.
