views:

363

answers:

2

Wondering if anyone can assist

I am utilizing some code from RIAForge which integrates with the Last.fm api...

One of the methods outputs as a struct, but I would like to modify the code so it outputs as an array, am unsure of how to do this..

Currently the code is like this

<cfscript>
 var args = StructNew();
 var returnStruct = StructNew();
 var results = "";
 var i = 0;


 args['playlistURL'] = arguments.playlistURL;

 results = super.callMethod('playlist.fetch', args).playlist;


 returnStruct['title'] = results[':title'];
 returnStruct['annotation'] = results[':annotation'];
 returnStruct['creator'] = results[':creator'];
 returnStruct['date'] = results[':date'];


 if(StructKeyExists(results, ':trackList') AND StructKeyExists(results[':trackList'], ':track')){
 results = super.ensureArray(results[':trackList'][':track']);

 returnStruct['tracks'] = QueryNew('album,creator,duration,identifier,image,info,title');

 for(i=1; i LTE ArrayLen(results); i=i+1){
 QueryAddRow(returnStruct.tracks);
 QuerySetCell(returnStruct.tracks, 'album', results[i].album);
 QuerySetCell(returnStruct.tracks, 'creator', results[i].creator);
 QuerySetCell(returnStruct.tracks, 'duration', results[i].duration);
 QuerySetCell(returnStruct.tracks, 'identifier', results[i].identifier);
 QuerySetCell(returnStruct.tracks, 'image', results[i].image);
 QuerySetCell(returnStruct.tracks, 'info', results[i].info);
 QuerySetCell(returnStruct.tracks, 'title', results[i].title);
 }
}
 return returnStruct;

Am just wondering if there is a coldfusion method that allows me to convert the returnStruct into a query..

Many thanks

+1  A: 

You'll need to do it by hand by looping over your results and placing in an Array of Arrays. If, you want to convert your struct to a query there are functions at http://www.cflib.org that are ready to go.

var returnArray = []; /* or arrayNew(1) if not on Railo or CF9 */

/* ACF9 or Railo Style */
arrayAppend(returnArray, [results[':title'],results[':annotation'],results[':creator'],results[':date'] ]);

/* ACF8 and earlier */
arrayAppend( returnArray, arrayNew(1) ]);
arrayAppend( returnArray[ arrayLen(returnArray) ], results[':title'] ]);
arrayAppend( returnArray[ arrayLen(returnArray) ], results[':annotation'] ]);
Aaron Greenlee
Hi thanks for response...when i try the code arrayAppend(returnArray, [results[':title'],results[':annotation'],results[':creator'],results[':date'] ]; I get the error message "Invalid token ;". Thanks
namtax
If your not on CF9 or Railo, you'll need to do the code in /* ACF8 and earlier */
Aaron Greenlee
Had the same issue with that code...had to remove an extra ']' from the end of the function line. Will have a look into this..thanks
namtax
A: 

CFLIB.org is your friend

QueryToArrayOfStructures

rip747