This question already has an answer here:
Do-while in Expect script
(1 answer)
Closed 8 years ago.
Is there any instruction in "Expect" Scripting which performs a do-while operation?
If so please share the details
Expect is an extension of TCL. TCL defines the while-loop. The TCL syntax is a little different than other languages, so you'll need to make sure your conditional evaluates properly--but conceptually, loops are loops, regardless of the language.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've seen many times that the overhead of Kernel#` and system(...) themselves can be a bottleneck in scripts. There are sometimes ways of reducing the overall # of shell invocations, e.g. with xargs, but sometimes I do need to invoke the shell many times in a row. What's the fastest way to do this? (I don't need any special environment variable setup or anything like that, nor even always the result code or stdout)
Remember there's two ways to run an external process:
With system("script with arguments") where that must pass through the shell first.
With system("script", "with", "arguments") where the command is run directly, bypassing the shell. This can be slightly faster if spinning up a shell is expensive, which it can be depending on your shell's configuration.
Note that both of these involve a fork/exec cycle that cannot be avoided.
If you need to do a lot of processing you might want to look into how xargs can work for you, especially when you can feed in a list of files/arguments via Open3. If you can somehow separate the output, which could be simple or hard, you can get a lot of mileage out of one shell process.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am taking a Compiler Design course in my undergraduate studies. As a part of the learning process, I'd have to develop the compiler for a language.
Can a compiler be written for Bash?
Would it be more difficult than designing a compiler for a regular programming language, like C/C++ and thus outright inconceivable, at least for a newbie?
Can a compiler be written for Bash?
Yes. (Existence proof - shc.)
If yes, how?
That's the hard part.
POSIX shell languages are very different to typical programming languages because of the effects of things like backticks, variable substitution, quoting, and so on.
You could ignore this and implement a "bash like" language, either leaving out the difficult features, or treating them in a way that doesn't conform to POSIX behavior.
Then ... there is the problem of how to generate something that is executable. Again, that is possible (see above), but if your aim is to be faster than a regular shell then you need to do things like emulating the behavior of common Linux commands in the compiled code. That is a huge task.
I'm not saying this is a bad project, but you will need to do a lot of work, including:
finding, reading and (fully) understanding the POSIX shell specs
researching how to implement a parser that deals with POSIX idiosyncracies
figuring out which linux commands need to be implemented directly, and
figuring out how to deal with the ones that you don't; e.g. all the complexity of pipelines.
This question already has answers here:
What algorithm does Python's built-in sort() method use?
(2 answers)
Closed 7 years ago.
I have always heard that usually merge sort is used for in built functions. But I cannot understand why. Quicksort is faster obviously with exact time complexity of 1.39NlogN and also it is in place! then why not quick sort?
timesort algo is used in the inbuilt sort
This question already has answers here:
Quick Sort in Ruby language
(6 answers)
Closed 7 years ago.
I'm having trouble wrapping my head around a in-place quick sort. I understand it using sub-arrays, but in-place is really throwing me.
Having an example in Ruby would really help, but I haven been able to find one. Could some one provide me with an example or point me in the right direction?
Rather than trying to understand it in code, there's a number of very elegant explanations of the quicksort algorithm out there. Here are a few of my favorite illustrations which may help your understanding.
With cups
With Hungarian Dance
As for a Ruby example, the answers to this question cover that.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I know that there are a couple ways to loop in various languages - a "for" loop, a "while" loop, a "do-while" loop, and just "loop" in Ruby for example. And I know there are various functions in every language that are pre-written in that language - for example the .each function in Ruby (which I think is based on a "for" loop and written entirely in Ruby and is replicable just using the language).
But what is the logic behind loops? Are they programmed from control flow statements in Assembly or even binary? And in fact, now that i think about it, what is the origin of all programming structures in general (such as variable name/value associations, arrays, hashes, etc - sorry if my terminology is wrong). Can anyone recommend sources to read more on this?
Loops in general are jumps in the program flow. When you compile your code i.e. in C, it gets "converted" to assembly, where you can see that loop structure:
So you start at a certain adress and do some stuff (i.e. ADD) which basically is everything inside your loop. At the very end then is a jump instruction back to the adress where you started. Breaking is is then i.e. made through a conditional jump inside your loop and thus you don't get to the jump instruction again and therefore do not loop again.