473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Shell-like case

In Bourne shell, you can do:

case ($x) in
foo*)
;;
*bar)
;;
esac

so that the first case matches any string starting with "foo", the
second any string ending in "bar", etc. In Tcl, you can:

switch -glob $x {
"foo*" {
}
"*bar" {
}
}

and accomplish the same thing. I'm struggling to do that in
JavaScript. switch seems to follow C semantics and do a full-length
match. And String.match() doesn't seem to do glob-style matching so I
can't do:

if ($x.match("foo*")) {
...

Is there a way to match on patterns in a JavaScript control structure?

Feb 20 '07 #1
7 1329
VK
On Feb 20, 4:11 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
In Bourne shell, you can do:

case ($x) in
foo*)
;;
*bar)
;;
esac

so that the first case matches any string starting with "foo", the
second any string ending in "bar", etc. In Tcl, you can:

switch -glob $x {
"foo*" {
}
"*bar" {
}
}

and accomplish the same thing. I'm struggling to do that in
JavaScript. switch seems to follow C semantics and do a full-length
match. And String.match() doesn't seem to do glob-style matching so I
can't do:

if ($x.match("foo*")) {
...

Is there a way to match on patterns in a JavaScript control structure?
If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:

var probe = 'foobar';

switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');

// other cases

case /bar$/.test(probe) :
window.alert('Executing statement 2');

break; default:
window.alert('No match found');
}

Feb 20 '07 #2
On Feb 20, 10:17 am, "VK" <schools_r...@yahoo.comwrote:
On Feb 20, 4:11 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
...
Is there a way to match on patterns in a JavaScript control structure?

If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:
No, my objective is glob-style or RE-style matching against the string
rather than exact matches.
var probe = 'foobar';

switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');

// other cases

case /bar$/.test(probe) :
window.alert('Executing statement 2');

break; default:
window.alert('No match found');

}
But your example seems to accomplish my goal. switch(true) is a
rather unusual construct I'd not likely have come up with soon.
Thanks.

Feb 20 '07 #3
On Feb 20, 3:32 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
On Feb 20, 10:17 am, "VK" <schools_r...@yahoo.comwrote:
On Feb 20, 4:11 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
...
Is there a way to match on patterns in a JavaScript control structure?
If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:

No, my objective is glob-style or RE-style matching against the string
rather than exact matches.
var probe = 'foobar';
switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');
You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...

// other cases
case /bar$/.test(probe) :
window.alert('Executing statement 2');
break; default:
window.alert('No match found');
}


Feb 21 '07 #4
On Feb 21, 4:58 am, n...@chthonic.f9.co.uk wrote:
On Feb 20, 3:32 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
On Feb 20, 10:17 am, "VK" <schools_r...@yahoo.comwrote:
On Feb 20, 4:11 pm, "Christopher Nelson" <cnel...@nycap.rr.comwrote:
...
Is there a way to match on patterns in a JavaScript control structure?
If I understood the objective correctly - thus to get "optional fall-
through" rather than separate if-else branches then:
No, my objective is glob-style or RE-style matching against the string
rather than exact matches.
var probe = 'foobar';
switch (true) {
case /^foo*/.test(probe) :
window.alert('Executing statement 1');

You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...
...
Yes, I think that's what he/she meant and no, that's not what I want
and I've got the break statements.

Feb 21 '07 #5
dd
On Feb 20, 4:17 pm, "VK" <schools_r...@yahoo.comwrote:
switch (true) {
case /^foo*/.test(probe) :
case /bar$/.test(probe) :
I soooo love that. I tip my hat off to you
for that solution. Switch(true) .... :)

Feb 22 '07 #6
Lee
Christopher Nelson said:
String.match() doesn't seem to do glob-style matching so I
>can't do:

if ($x.match("foo*")) {
...
if ( /foo.*/.test(x) ) {
...

Note that the regular expression /foo*/ matches "fo" followed
by any number of additional "o"s. It will not match "foob".
--

Feb 22 '07 #7
VK
On Feb 21, 12:58 pm, n...@chthonic.f9.co.uk wrote:
You'll need a break; statement here or strings
starting with foo will execute both statement 1
and statement 2. VK did this deliberately as
he thought that was what you wanted, or at least
I think that is what "to get "optional fall-through""
means...
Yes, this is what I meant.
"optional entry point for fall-through statements" would be more clear
maybe. Overall I'm struggling for a while to find a clear nominative
definitions - so noun or gerundive based like "foobarish foobar" or
"foobarish foobaring" - for switch and if-else blocks highlighting
their default behavior difference.

P.S. As it was not OP's objective then if-else if-else could be more
convenient rather than seal each case by break; but it seems failing
into personal preferences.

Feb 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mohsin | last post by:
Hi all, I have a perl program which makes a user exit to the O/S (unix, solaris) to issue a O/S command. I know that the shell it invokes is NOT a korn shell, because I captured the shell info...
0
by: Trips | last post by:
Hello Folks I have been having headache solving this and now I need your help I have developed an windows application which access network resources under differnt authenticated identity and...
5
by: gjuro kladaric | last post by:
it was not possible to make a shell extension from within VB2003, I believe has anything changed since then, can I (easily) write a VB code that would function as a shell extension thank you ...
6
by: Pieter | last post by:
Hi, I'm trying to use the Edanmo Shell Extension Library (http://www.mvps.org/emorcillo/en/code/shell/shellextensions.shtml) to make a Context Menu in the Windows Explorer with VB.NET 2005. It...
12
by: Dixie | last post by:
Is there a way to shell to Microsoft Word from Access and load a specific template - using VBA? dixie
8
by: Mike | last post by:
Am trying to open a Microsoft Word .doc file using Access 2000 with Shell function (on Windows XP Operating system) Here is the code : Shell "C:\Program Files\Microsoft...
3
by: George Sakkis | last post by:
I'm trying to figure out why Popen captures the stderr of a specific command when it runs through the shell but not without it. IOW: cmd = if 1: # this captures both stdout and stderr as...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
5
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort,...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.