Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays

Status
Not open for further replies.

sanjs

Technical User
Mar 2, 2003
28
0
0
GB
Hi,

Could someone please tell me what is wrong with this loop, I am trying to convert a variable to an array:

Where:
howold = ["2004, 5, 26, 13, 23, 0, 0","2004, 5, 26, 13, 23, 0, 0","2004, 5, 26, 13, 23, 0, 0"];


for (j=0; j<4; j++) {
atemp = howold[j];
var temp = [];
var begin = 0;
var firstSpace = atemp.indexOf(",");
var lastSpace = atemp.lastIndexOf(",");
for (var i = 0; i<atemp.length; i++) {
if (end != lastSpace) {
end = atemp.indexOf(",", begin+1);
temp = atemp.substring(begin, end, end-begin);
begin = atemp.indexOf(",", end)+1;
} else {
break;
}
}
temp.push(atemp.substring(lastSpace+1, atemp.length));
my_date = new Date(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6]);
todate.push(my_date.dateDiff("h", now));
}

What currently happens is the although it picks each array from howold each time, it does not go further split the string to an array after the first time.

Any help is welcome.

Regards,

Sanjay
 
Can't you just use 'split' to create the mini arrays? Much simpler in this case:

Code:
temp=atemp.split[","]

...will create an array keyed temp[0], temp[1] etc from the string.
 
Thanks wangbar,

I tried that but cannot seem to get it working:

Here is the function I am using:

for (i=0; i<records; i++) {
atemp = howold;
trace("aTemp Value "+atemp);
temp = [];
temp = atemp.split[","];
trace("Temp Array : " + temp);
my_date = new Date(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6]);
todate.push(my_date.dateDiff("h", now));
trace(todate);
}


here is the traces in the Output Window:

aTemp Value 2004, 5, 28, 13, 23, 0, 0
Temp Array :
0
aTemp Value 2004, 5, 27, 13, 23, 0, 0
Temp Array :
0,0
aTemp Value 2004, 5, 26, 13, 23, 0, 0
Temp Array :
0,0,0

Don't see why it does not work?

Regards,

Sanjay
 
Sorry - my fault. Use () around the string delimiter not [] as I did. Had arrays on the brain obviously...

Code:
   temp = atemp.split(",");
 
Thanks wangbar, now works perfectly.

Regards,

Sanjay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top