Is the Order of Iteration in Bash for Loop Guaranteed

Is the order of iteration in bash for loop guaranteed?

According to the bash man page:

Pathname Expansion

After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these characters
appears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of filenames matching the pattern (see
Pattern Matching below).

So, the files will be processed in alphabetical order (which depends on your locale, see comments) due to globbing expansion rules.

Does the for/in loop construct preserve order?

A for loop's iteration order is controlled by whatever object it's iterating over. Iterating over an ordered collection like a list is guaranteed to iterate over elements in the list's order, but iterating over an unordered collection like a set makes almost no order guarantees.

Do Java Iterators or Enhanced Loops guarantee results in order of creation?

Java doesn't provide any guarantees for these however, in certain cases, items will be iterated in a particular order:

  • Lists and arrays will iterate over elements in order
  • TreeSet in sort order
  • LinkedHashSet in creation order

It all depends on what you're iterating over, and the particular implementation.

Is a For Loop always executed at least once?

You could say a for-loop is always evaluated at least once.

But if a for-loop's condition is not met, its block will never execute.

Because you didn't ask about other loops, I won't address those.

Is the .each iterator in ruby guaranteed to give the same order on the same elements every time?

This depends on the specific Enumerable object you are operating on.

Arrays for example will always return elements in the same order. But other enumerable objects are not guaranteed to behave this way. A good example of this is the 1.8,7 base Hash. That is why many frameworks (most notably ActiveSupport) implement an OrderedHash.

One interesting side note: Even Hash will return objects in the same order if the hash has not changed between each calls. While many objects behave this way, relying on this subtlety is probably not a great idea.

So, no. The generic each will not always return objects in the same order.

P.S. Ruby 1.9's hashes are now actually ordered http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash

What sort order does Linux use?

The sort order for many commands (incl. bash glob, ls, sort) is based on your current locale settings.

You can force the collation by setting the LC_COLLATE environment variable. Setting it to C will perform a comparison on byte values.

On my system (en_US.utf8):

sh$ touch eleve
sh$ touch élève
sh$ touch Eleve
sh$ touch Élève
sh$ touch äkta
sh$ touch österreich

sh$ ls
äkta eleve Eleve élève Élève österreich pommes

sh$ LC_COLLATE=fr_FR.utf8 ls
äkta eleve Eleve élève Élève österreich pommes

sh$ LC_COLLATE=sv_SE.utf8 ls
eleve Eleve élève Élève pommes äkta österreich

sh$ LC_COLLATE=C ls
Eleve eleve pommes Élève äkta élève österreich

Why the output of array using awk is not in right order?

awk doesn't actually have indexed arrays; it only has associative arrays. This means you can't iterate over the keys in an guaranteed order. split, however, does promise that the array it populates will use the numbers 1 through n as the keys. This means you can iterate over the correct numerical range, and use those to index the array.

for (i=1; i<=length(text_arr); i++) {
print text_arr[i];
}


Related Topics



Leave a reply



Submit