Here is a tutorial that follows up on yesterday’s C++ Xcode Numerical Bubble Sort Tutorial as shown below, in an “oh, I forgot” moment to make the link between the programming language C (and then C++) and its relationship to the Linux or unix shell scripting environment C Shell (we’ll be saying CSH) (ie. today we go from a compiling language to an interpretive scripting set of languages), and CSH’s relationship with (the remarkable, the wonderful, the stupendous) awk, making functionality like numerical sorts quite easy just using shell scripting functionality. Well, that’s how it started with work on the Linux command line here on this Mac laptop using the Terminal application. But, and am so very very very sorry to start a sentence with but … but, well you see there’s this … but I digress … I thought this could become a webpage or web application pretty easily by involving our old favourite PHP/exec combination (with code rewritten for Korn Shell (we’ll be saying KSH)), because the rjmprogramming.com.au domain prefers KSH Korn shell … so we proceeded along those lines … and then lo and behold, it was apparent this could also be written for all the three PHP “modes of use” (latest “foot in the door” tutorial for this would be PHP Modes of Use File Traverse Tutorial) we’ve been talking about here lately at this blog … namely PHP used with curl and the command line usage and the web browser usage.
Attempting to write code with a generic eye has the advantage that you end up with more flexibility, and, usually, more readable code.
So in the CSH scripting code you’ll (need, maybe, to open up execute privileges via a command like chmod 755 ./bubble_sort.*sh and) see:
- The use of set via set variable=value syntax … will fit in with all those lovers of interpretive languages (who like to add $ … (who doesn’t?))
- The use of Command line arguments as with ./bubble_sort.csh 22 2 15 0 … adds to the flexibility of your code with the user being able to tailor each usage of the script
- The CSH (and very C like) way of incrementing numerical variables via @ variable++ syntax
- The use of the underlying Linux operating system command to (numerically) sort (ie. sort -n[r]) … whether it uses a “bubble sort” am not totally sure … but if not try the “buble sort” … chortle, chortle
- The deliberate pointing out of any important restriction should there be one … doh! … in that we have a stipulation for a numerical data sort
Found this link very useful for C shell (ie. CSH) research … thanks.
Link to some downloadable CSH programming code … rename to bubble_sort.csh … okay at Linux command line at this Mac, at least … but for rjmprogramming.com.au web server usage needed …
Link to some downloadable KSH programming code … rename to bubble_sort.ksh … the differences described by bubble_sort.csh
Link to some downloadable PHP programming code … rename to bubble_sort.php … which can supervise bubble_sort.ksh above using PHP exec command in the, aforementioned, “modes of use”:
- Via http transport layer (ie. web browsing, or “surfing the net”) in an Internet mode of use (eg: http://www.rjmprogramming.com.au/Linux/awk/csh/bubble_sort.php?jsize=12&jmin=37&jmax=82&imode=0) in address bar of a web browser
- Via curl in an Internet mode of use (eg. curl http://www.rjmprogramming.com.au/Linux/awk/csh/bubble_sort.php?jsize=12@37@82@0) at Linux or unix or Windows command line
- Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php ./bubble_sort.php 12 37 82 0) at a Linux or unix or Windows command line
Previous relevant C++ Xcode Numerical Bubble Sort Tutorial is shown below.
Here is a tutorial that uses the Xcode IDE on a Mac laptop to create a C++ desktop compiled application using a Bubble Sort method to numerically sort some numbers (ie. double).
Attempting to write code with a generic eye has the advantage that you end up with more flexibility, and, usually, more readable code.
Some ideas used in today’s code include:
- The use of Preprocessor directives via #define identifier replacement syntax … will fit in with all those lovers of interpretive languages
- The use of Command line arguments as with ./Test 22 2 15 0 … adds to the flexibility of your code with the user being able to tailor each usage of the executable
- The use of a pointer to an array within a function, meaning that the data can be changed (ie. sorted) in situ rather than returning a value (or resorting to a global variable array) … hence we use void numerically_sort_array(double* asort, int isize = ASIZE, bool ascending = (SMODE != 0)) … you could change the void return, to return an error code, for example
- The C++ use of default values in parameters of the function call, making use of the Preprocessor directives, so that the call could make sense as numerically_sort_array(psarray); as numerically_sort_array(psarray, 1000, true); … such thoughts come into play, in C++, also when considering Overloading
- The deliberate pointing out of any important restriction should there be one … doh! … in that we have a stipulation for a double data type array … and have not implemented any Template functionality, on this occasion
Link to some downloadable C++ programming code … rename to main.cpp for use.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
8 Responses to CSH/KSH/PHP Numerical Bubble Sort Tutorial