views:

398

answers:

1

Not sure if any of you guys/girls out there that uses the NZBMatrix website API..

In short what I'm trying to do is build an Adobe Air Application, using JavaScript, AJAX to connect to the API with a search query, this is all good.

When i receive the "request.responseText" back from the API with the 5 results (can only be 5) I'm having trouble with the JavaScript split function trying to split them all out...

the return string is returned as follows:

NZBID:444027;
NZBNAME:test result 1;
LINK:nzbmatrix.com/nzb-details.php?id=444027&hit=1;
SIZE:1469988208.64;
INDEX_DATE:2009-02-14 09:08:55;
USENET_DATE:2009-02-12 2:48:47;
CATEGORY:TV > Divx/Xvid;
GROUP:alt.binaries.test;
COMMENTS:0;
HITS:174;
NFO:yes;
REGION:0;
|
NZBID:444028;
NZBNAME:another test;
LINK:nzbmatrix.com/nzb-details.php?id=444028&hit=1;
SIZE:1469988208.64; = Size in bytes

etc..etc..

the first Array should split each set of results using | assign those 5 results to a new array.

the 2nd Array should split each value using : assign those 12 results to new variables ie: var nzbidtxt = array1[0]; which would echo like: document.write(nzbidtxt); // ie: print "NZBID:"

the 3rd Array should split each variable from ; assign those 12 values to the newly created array ie: var nzbidValue = array2[0]; which would echo like: document.write(nzbValue); // ie: print "444027"

so using both arrays I can display a listing of the posts returned.. in a nice usable format..

nzbid: 444027 // this will be used for direct download nzbName: the name of the nzb etc..etc..

the function i have been working on is below:

function breakNzbUrlResponse(text)
{
    var place = new Array;
    var place2 =new Array;
    var place3 =new Array;

    place[0] = text.indexOf('|');
    place2[0] = text.indexOf(':');
    place3[0] = text.indexOf(';');

    var i = 1;
    while(place[i-1] > 0 || i==1) {
     place[i] = text.indexOf('|',place[i-1]+1);
     place2[i] = text.indexOf(':',place2[i-1]+1);
     if(place2[i] == -1)
      {
      place2[i] = text.length;
     }
     i++;
    }
    i=1;
    var vars = new Array;
    var values = new Array;
    var retarray = new Array;
    vars[0] = text.substr(0,place[0]);
    values[0] = text.substr((place[0]+1),((place2[0]-place[0])-1));
    retarray[vars[0]] = values[0];
    while(i < (place.length-1) || i==1)
     {
     vars[i] = text.substr((place2[i-1]+1),((place[i]-place2[i-1])-1));
     values[i] = text.substr((place[i]+1),((place2[i]-place[i])-1));
     //alert('in loop\r\nvars['+i+'] is: '+vars[i]+'\r\nvalues['+i+'] is: '+values[i]);
     retarray[vars[i]] = values[i];
     i++;
    }
    return retarray;
}

This feels and looks like a very long winded process for this type.. all I want to do is basically assign a new variable to each return type ie

 var nzbid = array3[0];

which when split would reference the first line of the return string, NZBID:444027; where the value for NZBID would be 44027..

bit of a book going on, but the more info the better i suppose. Thanks Marty

+1  A: 

You could probably cut out a significant number of lines of code by further utilizing split() instead of the manual dissections of the entries and using multidimensional arrays instead of repeatedly creating new arrays.

The logic would be:

ResultsArray = split by "|"  
FieldArray = Each element of FieldArray split by ";"  
ValueArray = Each element of FieldArray split by ":"
Mike Buckbee
Thanks Mike, in short that's what I would prefer to do, but going about this proved a little more difficult than i thought..
Marty