Function to turn Array into simple comma-separated list?

Questions, tips and tricks and techniques for scripting in Servoy

Function to turn Array into simple comma-separated list?

Postby john.allen » Sat Feb 16, 2013 6:42 am

I feel this is something I did years ago but can't seem to find the Servoy little function. I know I can turn say a dataset.getColumnAsArray(1) that looks like this '[123, 456, 678]' into this '123, 456, 678' with regex or array.shift stuff but I thought there was some simple Servoy function that made it even easier. I'm going to have to do this a lot (basically passing various 'IDs' to 'IN' statements) to simplify some queries against our huge hospital database and thought I'd ask... :)
John Allen
Stanford University
john.allen
 
Posts: 515
Joined: Wed Jul 02, 2003 10:07 pm
Location: Stanford CA USA

Re: Function to turn Array into simple comma-separated list?

Postby ptalbot » Sat Feb 16, 2013 8:50 am

Array.join(separator) is what you are looking for...

So you can do:
Code: Select all
var ids = dataset.getColumnAsArray(1).join(',');


Hope this helps,
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Function to turn Array into simple comma-separated list?

Postby john.allen » Sat Feb 16, 2013 9:44 am

Thanks Patrick. That's strange. That's what I thought it was, the join function. But when I did it to populate the IN statement it still failed and when I outputted the result it still had the square brackets surrounding it like an array. I did it in two lines getting the dataset column as an array and then doing the join with separator statement but I think pretty much the same thing. I'll try tomorrow. You don't get the square brackets with that? (Servoy 6.1.3)

John
john.allen
 
Posts: 515
Joined: Wed Jul 02, 2003 10:07 pm
Location: Stanford CA USA

Re: Function to turn Array into simple comma-separated list?

Postby ptalbot » Sat Feb 16, 2013 7:32 pm

Code: Select all
var array = [123, 456, 678];
application.output(array.join(','));

Gives me:
Code: Select all
123,456,678

So I don't know what you are doing but probably something wrong! ;)
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Function to turn Array into simple comma-separated list?

Postby john.allen » Sun Feb 17, 2013 12:28 am

Yup. Definitely something wrong. I tried doing 'undo' as far as I could to see how I phrased it but wouldn't go back that far. I hadn't used join in a long while but it is such a simple array function I can't see how I could have had it wrong. Must have grabbed the wrong variable to output and put in the IN clause I guess...
Thanks Patrick! :oops:
John Allen
Stanford University
john.allen
 
Posts: 515
Joined: Wed Jul 02, 2003 10:07 pm
Location: Stanford CA USA


Return to Methods

Who is online

Users browsing this forum: No registered users and 5 guests

cron