awk - function to return permutations of n items out of m

First of all, i am glad your problem was solved. There are still some points you might want to consider:

ahem - no! You said (#1):

Just because you tried we do not have to try the same - after all, you didn't (at that time) have success with what you tried. We often have questions like how can i do X with tool Y where Y is something we positively know to be not suited at all for achieving X. (You wouldn't believe how often i have been asked something analogous to "please explain how i can solder this to that using a refrigerator and the collected works of Shakespeare".) Now, i admit this is not the case with your question but if you need the solution to be in awk, then please say so.

Like this, which is a valid reason to stick with awk. But since you didn't tell us you can hardly blame us for not knowing this, no?

Two things: first, yes, in some cases, but these were merely meant as demonstrations for using a certain method - in this case a certain little-known form of parameter expansion - and it was obvious how to adapt that for an set and any number of elements in the combination set. Addition is like 1 + 1 = 2 doesn't insinuate that i think "2 + 2" to work any different and of course i could say: addition is a commutative and associative operation on either sets of scalar elements or vector spaces .... Somehow, i noticed, though, that many people prefer the first over the second.

Second: in case of my function (#15) this isn't even true, because you can use any set and any number of elements from the set to build combinations. For instance this invocation:

perm ()
{
myset="$1"
n="$2"
buf=""
i=1

while (( i <= n )) ; do
     buf="${buf}${myset}"
     (( i++ ))
done

for x in $buf ; do echo $x ; done

return 0
}

perm "{1,a,2,b}" 5

will give you 1024 lines from "1111" to "bbbb". You could even call the function (i tried only in ksh93 but suppose it to work in bash too) with arbitrary strings like this:

# perm "{"ab","XY"}" 3
ababab
ababXY
abXYab
abXYXY
XYabab
XYabXY
XYXYab
XYXYXY

You definitely have a point there. As i am about to post a larger software project for the community within the next days i suggest we should have a discussion about coding standards in general. Perhaps we could all profit somewhat from such a discussion and by seeing what others do and why.

bakunin

2 Likes

For speed I usually start with shorter names when I create code quickly, in this case I only had a couple of minutes..
This stems from coding on the command line, where I do most of my coding. Most of the time this is for ad hoc one-time scripts. Using short variable names is a real benefit there for speed and keeping it all in the head..

If code is working well then it makes it into a more permanent script and then of course the variable names are replaced by mnemonic variable names and larded with comments (although I am definitely not a fan of overdoing comments)

In this thread it is just about coding ideas, so that one can experiment what works best, not production level solutions. They are just tiny snippets of code, quick ideas, that should be easy to understand even with minimal mnemonics. I think it may be even be a benefit as it encourages people who want to reuse the code to really think about it and try to understand the meaning .

When I am coding for production scripts, I would not dream of using single letter variables other than for counters, obviously..
For larger software projects of course, or when coding in a team this is very important, and I am all for mnemonics and coding standards, but not so much for small snippets that are just rudimentary coding ideas ...

S.

2 Likes