Reputation: 25
I'm trying to allow for the manual deletion (or change of coordinates) of lines in an array through an manual input. when I plot the 'array.size' function, it shows the array size as 14, however when I try to turn off any of the lines in the array except the first (index = 0) it gives me this error: "In 'array.get()' function. Index 1 is out of bounds, array size is 1." Code:
var line [] line_array= array.new_line ()
if m_sr
line_supres = line.new (m_time, m_sr, (m_time+1), m_sr, xloc.bar_time, extend.right, color.green)
array.push (line_array, line_supres )
if (barstate.isrealtime == false) //confirmed
line.set_color (line_supres, color.purple)
if (tf_supres1 == false)
line.set_y1 (array.get (line_array, 0), 0)
line.set_y2 (array.get (line_array, 0), 0)
if (tf_supres2 == false)
line.set_y1 (array.get (line_array, 1), 0)
line.set_y2 (array.get (line_array, 1), 0)
if (tf_supres3 == false)
line.delete (array.get (line_array, 2))
if (tf_supres4 == false)
line.delete (array.get (line_array, 3))
if (tf_supres5 == false)
line.delete (array.get (line_array, 4))
if (tf_supres6 == false)
line.delete (array.get (line_array, 5))
if (tf_supres7 == false)
line.delete (array.get (line_array, 6))
if (tf_supres8 == false)
line.delete (array.get (line_array, 7))
if (tf_supres9 == false)
line.delete (array.get (line_array, 8))
if (tf_supres10 == false)
line.delete (array.get (line_array, 9))
if (tf_supres11 == false)
line.delete (array.get (line_array, 10))
if (tf_supres12 == false)
line.delete (array.get (line_array, 11))
if (tf_supres13 == false)
line.delete (array.get (line_array, 12))
if (tf_supres14 == false)
line.delete (array.get (line_array, 13))
if (tf_supres15 == false)
line.delete (array.get (line_array, 14))
if barstate.islast
label.new (bar_index, 0, "Array Size: " + str.tostring (array.size(line_array)))
Here are some plot screen shots:
1.) Plot with no lines turned off
2.) Plot with line 1 turned off (correctly changed coordinates to the 0 line)
3.) Plot with line 2 turned off (breaks chart with the error message for any lines 2-last)
Thanks so much in advance!
Upvotes: 1
Views: 1337
Reputation: 1362
Study Error
In 'array.get()' function. Index 1 is out of bounds, array size is 1.
What does it say?
We are trying to refer to an item of an array by index #1 and this number is not valid, since our array has only 1 item.
This is correct because the indexing in computer sciences starts by #0 and not by #1. So element 1 has index of #0, element 2 has index of #1 etc. but you probably know that.
Your problem in this case is the following:
On the very first bar where m_sr
is true, you are pushing the first element in your array. So your array has 1 element with index #0.
Then you're proceeding and tf_supres2 == false
is true since you turned it off (I suppose but I don't see your input) so you're trying to call array.get(line_array, 1)
but you don't have 2 elements yet, so index #1 is missing as you're trying to refer to that.
If you leave everything on, there's no problem since the lines can fill up the array. If you switch off the first one, no problem, since you always have at least 1 item in your array, so index #0 will be found.
Solution:
Change your conditions as follows: if tf_supres2 == false and array.size(line_array) > 1
(change the number respectively for the other conditionals)
Please provide complete or at least somewhat more complete code next time so that the community can provide you more precise information/support. It is difficult to help you without seeing everything.
Upvotes: 1