Array.reverse()

I am graphing data from data.sparkfun.com. using Google charts on the agents HTML page. I am using the following code to jsondecode the data into 7 arrays.

` local data = http.jsondecode(res.body);
//local dataS = data.datastreams;
cnt = 0;
try {

foreach(s in data) {

    T1Data[cnt] = s.t1.tofloat();
    T2Data[cnt] = s.t2.tofloat();
    T3Data[cnt] = s.t3.tofloat();
    T4Data[cnt] = s.t4.tofloat();
    TtbData[cnt] = s.boardtemp.tofloat();
    ThumData[cnt] = s.humidity.tofloat();
    T_d_Data[cnt] = s.timestamp;
    cnt++;

}
    server.log("Found " + cnt + " Records.")

}

catch(error) {
server.error(error);

}
`

I am using the following to attach the data to the html page:
`local _t1Data = array(500);
_t1Data = T1Data//.reverse();
local _t2Data = array(500);
_t2Data = T2Data//.reverse();
local _t3Data = array(500);
_t3Data =T3Data//.reverse();
local _t4Data = array(500);
_t4Data =T4Data//.reverse();
local _ttbData = array(500);
_ttbData = TtbData//.reverse();
local _thumData = array(500);
_thumData = ThumData//.reverse();
local _t_d_Data = array(500);
_t_d_Data = T_d_Data//.reverse();
local skipDates = 20;

//server.log(“Date: " + t_d_Data.readstring(24))//2015-10-28T20:55:50.942Z
try{
for (t = 0; t < cnt; t++){
//do{
//dtm = format(”%.1f",dt);
//dt = dt - .083;
if (t == skipDates){
skipDates += 20;
htmlresp += “[’” + _t_d_Data[t] + “’,” + _t1Data[t].tofloat() + “,” + _t2Data[t].tofloat() + “,” + _t3Data[t].tofloat() + “,” + _t4Data[t].tofloat() + “,” + _ttbData[t].tofloat() + “,” + _thumData[t].tofloat() + “],\r
”;
}
else{
htmlresp += “[’’,” + _t1Data[t].tofloat() + “,” + _t2Data[t].tofloat() + “,” + _t3Data[t].tofloat() + “,” + _t4Data[t].tofloat() + “,” + _ttbData[t].tofloat() + “,” + _thumData[t].tofloat() + “],\r
”;
}
//htmlresp += “[’” + req.body + “],\r
”;
//}while(_t1Data.len() > 0)

}

}
catch(error){

 server.error(error);

}`

The problem is when I use the .reverse() function the error reads that the index 0 does not exist anywhere I am trying to read the arrays in the for statement. If I take out the .reverse() the google line chart graph shows up fine on the page just the newest data is on the left instead of the right.

The reverse function reverses the array in-place but it looks like you are expecting it to return a reversed copy of the array. I think the code you want would look more like this:

local _t1Data = T1Data.clone(); //Create a copy of T1Data _t1Data.reverse(); //Reverse the copy

No matter if I use the .reverse using either style of programming, it tells me index of 0 does not exist after the reverse. If I leave out the .reverse, the array looks fine.

Could you attach your updated code? (Maybe without so many commented out lines, it makes it quite hard to read)

I just tried this:
`
local a = [“first”,“middle”, “last”];
local b = clone(a);

server.log(b[0]); //Logs "first"
b.reverse();
server.log(b[0]); //Logs “last”
`

and it worked exactly as expected.