tags:

views:

91

answers:

4

hello,

i'm searching for an equivalent function for php's "list()". i'd like to split a string with "split()" and put the array values in two different variables. hopefully there's another way to do this as short and performance-optimized as it could be.

thanks for your attention!

A: 

If you can live without the naming, you can do this:

var foo = ["coffee", "brown", "caffeine"];
foo[0] + " is " + foo[1] + " and  " + foo[2] + " makes it special";

Alternatively, use a object to name the keys:

var foo = {drink: "coffee", color: "brown", power: "caffeine"};
foo.drink + " is " + foo.color + " and " + foo.power + " makes it special";
August Lilleaas
A: 

I think the closest you can get is the explicit

var args = inputString.split(",");

// Error checking, may not be necessary
if (args.length < 3)
{
   throw new Error("Not enough arguments supplied in comma-separated input string");
}

var drink = args[0];
var color = args[1];
var power = args[2];

since Javascript doesn't have multiple assigment operators. I wouldn't worry too much about the efficiency of this; I expect that the list function in PHP basically boils down to the same thing as above, and is just syntactic sugar. In any case, unless you're assigning hundreds of thousands of variables, the time to execute anything like the above is likely to be negligible.

Andrzej Doyle
+1  A: 

Here's a list implementation in javascript:

http://solutoire.com/2007/10/29/javascript-left-hand-assignment/

CtlAltDel
+1  A: 

Maybe PHP.JS is worth looking at (for future stuff maybe). I noticed that the list() was only experimental though but maybe it works.

anddoutoi