Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30899 > unrolled thread

Any way to do this in JavaScript?

Started by$Bill <news@todbe.com>
First post2016-07-19 10:12 -0700
Last post2016-07-20 10:56 +0200
Articles 9 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Any way to do this in JavaScript? $Bill <news@todbe.com> - 2016-07-19 10:12 -0700
    Re: Any way to do this in JavaScript? Martin Honnen <martin.honnen@gmx.de> - 2016-07-19 20:14 +0200
      Re: Any way to do this in JavaScript? $Bill <news@todbe.com> - 2016-07-19 12:02 -0700
    Re: Any way to do this in JavaScript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-19 21:05 +0200
      Re: Any way to do this in JavaScript? $Bill <news@todbe.com> - 2016-07-19 12:49 -0700
        Splitting strings (was: Any way to do this in JavaScript?) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-20 00:25 +0200
          Re: Splitting strings (was: Any way to do this in JavaScript?) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-07-20 09:45 +0200
    Re: Any way to do this in JavaScript? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-07-19 23:21 -0700
      Re: Any way to do this in JavaScript? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-07-20 10:56 +0200

#30899 — Any way to do this in JavaScript?

From$Bill <news@todbe.com>
Date2016-07-19 10:12 -0700
SubjectAny way to do this in JavaScript?
Message-ID<nmln2h$kbr$1@dont-email.me>
Sent this to alt... by mistake - let's try here.

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?

[toc] | [next] | [standalone]


#30901

FromMartin Honnen <martin.honnen@gmx.de>
Date2016-07-19 20:14 +0200
Message-ID<nmlqm3$gms$1@news.albasani.net>
In reply to#30899
On 19.07.2016 19:12, $Bill wrote:
> Sent this to alt... by mistake - let's try here.
>
> 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?

See 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment, 
in Firefox and Chrome you should be able to use e.g.

   var [title, chan, call, sTime, dur] = el.split('~~');

See https://jsfiddle.net/rxjwajpn/.

[toc] | [prev] | [next] | [standalone]


#30906

From$Bill <news@todbe.com>
Date2016-07-19 12:02 -0700
Message-ID<nmltgm$ctv$1@dont-email.me>
In reply to#30901
On 7/19/2016 11:14, Martin Honnen wrote:
> On 19.07.2016 19:12, $Bill wrote:
>>
>>     var (title, chan, call, sTime, dur) = el.split('~~'); // do it all
>> in one like Perl
>>
>> Any way to do that?
>
> See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment, in Firefox and Chrome you should be able to use e.g.
>
>   var [title, chan, call, sTime, dur] = el.split('~~');
>
> See https://jsfiddle.net/rxjwajpn/.

I knew there had to be a way to do that - kind of an obscure part of the reference
manual though.  I'm looking under var and assignment type ref pages.

Much grass amigo, that worked perfectly.

[toc] | [prev] | [next] | [standalone]


#30908

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-07-19 21:05 +0200
Message-ID<1889594.irdbgypaU6@PointedEars.de>
In reply to#30899
$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('~~');

<http://PointedEars.de/scripts/test/es-matrix/?filter=%5D%5Cs%2B%3D%20Array>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment> 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.

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>


Next time, post with your real name.  This is Usenet, not a chat room.

__________
[1] <http://PointedEars.de/wsvn/JSX/trunk/object.js>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#30916

From$Bill <news@todbe.com>
Date2016-07-19 12:49 -0700
Message-ID<nmm07r$mmi$1@dont-email.me>
In reply to#30908
On 7/19/2016 12:05, Thomas 'PointedEars' Lahn wrote:
>
> 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('~~');
>
> <http://PointedEars.de/scripts/test/es-matrix/?filter=%5D%5Cs%2B%3D%20Array>
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment> 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

Thanks for your further clarification of the issue.

> 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.

If you're referring to the split delimiter/pattern, it can be an RE or a string in Perl.

> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>
>
>
> Next time, post with your real name.  This is Usenet, not a chat room.

It's a nickname.  I used it for 21 years at JPL/NASA and it was good enough for them ...
I earned it at Litton - Data Systems Division - it actually is supposed to be a 1 and S
superimposed and I used it to initial in tiny boxes back in my operation days and I
attached it to my first name for brevity in submitting jobs later on.  I plan on using
it till I succumb to the environment and leave this plane - if that's all the same to you. ;)

[toc] | [prev] | [next] | [standalone]


#30921 — Splitting strings (was: Any way to do this in JavaScript?)

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-07-20 00:25 +0200
SubjectSplitting strings (was: Any way to do this in JavaScript?)
Message-ID<7188767.T7Z3S40VBb@PointedEars.de>
In reply to#30916
$Bill wrote:

> On 7/19/2016 12:05, Thomas 'PointedEars' Lahn wrote:
>
> Thanks for your further clarification of the issue.

You’re welcome.
 
>> 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.
> 
> If you're referring to the split delimiter/pattern, it can be an RE or a
> string in Perl.

You are missing the point:

| $ perl -We 'use strict; print(join(",", split("o+", "foobar")), "\n");'
| f,bar
| 
| $ perl -We 'use strict; print(join(",", split(/o+/, "foobar")), "\n");'
| f,bar
| 
| $ perl -v | head -n 2
| 
| This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-
| gnu-thread-multi

vs.

| $ js -e '"use strict"; console.log("foobar".split("o+").join(","));'
| foobar
| 
| $ js -e '"use strict"; console.log("foobar".split(/o+/).join(","));'
| f,bar
| 
| $ js --help | head -n 1
| Usage: node [options] [ -e script | script.js ] [arguments] 
| 
| $ js -v
| v0.10.38

>> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>

Details can be found there and resources referred there.

>> Next time, post with your real name.  This is Usenet, not a chat room.
> 
> It's a nickname.  I used it for 21 years at JPL/NASA

Good for you.

> and it was good enough for them ... […]

As you can see in my postings, nicknames are used differently in Usenet 
(nowadays).

Also, you should choose the Subject header field value more wisely next time 
(as I should have earlier).  See the FAQ on posting for details.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#30929 — Re: Splitting strings (was: Any way to do this in JavaScript?)

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-07-20 09:45 +0200
SubjectRe: Splitting strings (was: Any way to do this in JavaScript?)
Message-ID<XnsA64B6344D3824eejj99@194.109.6.166>
In reply to#30921
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 20 Jul 2016 in
comp.lang.javascript: 

> As you can see in my postings, nicknames are used differently in Usenet 
> (nowadays).

Seems, Thomas, you are splitting hairs instead of strings.

Good luck to you.

> Also, you should choose the Subject header field value more wisely next
> time (as I should have earlier).  See the FAQ on posting for details.

Agree. The subject line should declare the subject at hand, 
not "Read my subject in the text."

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#30926

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-07-19 23:21 -0700
Message-ID<aee721b8-10bc-4946-8d04-85b786e13bec@googlegroups.com>
In reply to#30899
On Tuesday, July 19, 2016 at 12:12:56 PM UTC-5, $Bill wrote:
> Sent this to alt... by mistake - let's try here.
> 
> 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?

Yes:

var [a, b, c] = "1,2,3".split(",");

YMMV based on environment of course.

[toc] | [prev] | [next] | [standalone]


#30930

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-07-20 10:56 +0200
Message-ID<XnsA64B6F5693ACAeejj99@194.109.6.166>
In reply to#30926
"Michael Haufe (TNO)" <tno@thenewobjective.com> wrote on 20 Jul 2016 in 
comp.lang.javascript:

> var [a, b, c] = "1,2,3".split(",");

Nice!

==================

No equal array-lengths necessary [Chrome tested]:

var weekdaysplus = 
"Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,";
var warray = weekdaysplus.split(/,/)

var [mo,tu,we,th,fr,sa,su,xx,yy] = warray;
document.write([fr,mo,su].join(' =1= '));

document.write('<br>' + typeof xx);

document.write('<br>' + typeof yy);

var [a,b,c] = warray;
document.write('<br>' + [a,b].join(' =2= '));

var [,,a,b,c] = warray;
document.write('<br>' + [a,b,c].join(' =3= '));

gives:

Friday =1= Monday =1= Sunday
string
undefined
Monday =2= Tuesday
Wednesday =3= Thursday =3= Friday

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web