Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Any way to do this in JavaScript? Date: Tue, 19 Jul 2016 21:05:21 +0200 Organization: PointedEars Software (PES) Lines: 65 Message-ID: <1889594.irdbgypaU6@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: solani.org 1468955126 11808 eJwFwQcBwDAMAzBKc15bOHnmD2GSayD6WHiY01ksfxDdu7Nl+KRBqbRJzgSA+zT5xNh5Tv0yYxHk (19 Jul 2016 19:05:26 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 19 Jul 2016 19:05:26 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYEBwCAIA7CXZqGg5yDS/09YQosVnR4Mp6izMYOpmx+no0/rHWFEuwUT8oU3bEXeBj+B6at6l0H09wNy1hXy Cancel-Lock: sha1:HkiDGNq34cBWVFPYN+ttCKjhXXc= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BLvOmQ0wLxL2F36VD0CSQimVzhHrtfaPnWG4zBEFONoHCkNE292+vqvv4BHKERMQ== Xref: csiph.com comp.lang.javascript:30908 $Bill wrote: > In Perl, I have: > > my ($title, $chan, $call, $sTime, $dur) = split /~~/, $el; > > I've converted this to JavaScript thusly: > > var flds = el.split('~~'); // split into array, then to vars > > var title = flds[0]; > var chan = flds[1]; > var call = flds[2]; > var sTime = flds[3]; > var dur = flds[4]; > > I was hoping for a more straightforward/cleaner syntax - something like: > > var (title, chan, call, sTime, dur) = el.split('~~'); // do it all in > one like Perl > > Any way to do that? Conforming implementations of ECMAScript 2015 support the destructuring assignment, formerly introduced for arrays only with Mozilla JavaScript 1.7: var [title, chan, call, sTime, dur] = el.split('~~'); pp. If one of the implementations that you need to support does not have that feature, you can use something like my jsx.array.destructure() [1]: var obj = jsx.array.destructure(el.split('~~'), ["title", "chan", "call", "sTime", "dur"]); Afterwards there are enumerable, writable, and configurable properties, respectively: obj.title obj.chan obj.call obj.sTime obj.dur Note that while in Perl your delimiter is always considered a regular expression (see perlfunc(1)), in ECMAScript implementations it can be either a string value or a regular expression. In this case, using a string value as you did is the proper, most efficient, approach. Next time, post with your real name. This is Usenet, not a chat room. __________ [1] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.