awk - function to return permutations of n items out of m
Hi,
I'm trying to write an awk function that returns all possible permutations of n items chosen in a list of m items. For example, given the input "a,b,c,d,e" and 3, the function should return the following :
(125 lines = 5 ^^ 3).
I've managed to write a function with nested loops that works, but only for a predetermined depth. I can't seem to manage the recursion if I want n to be variable.
Also, an added difficulty is that I need the results in an array for further processing in the same script, not just output to the console.
Thanks for your prompt response. I didn't post my code as it's not really much use. I've also searched this forum, but none of the threads about combinations and/or permutations does exactly what I need.
Here's what I have so far. It's not in a function as yet, and the loops are hard-coded two deep. The variable len isn't used at all.
Ultimately, I need to be able to access and manipulate the results in an array.
What I'm writing now is important for programs with a lot more code, but I think it has benefits in small (awk) programs in this forum too.
Descriptive code
For me - and probably for others like the partly beginner level thread starters more alike - non-descriptive variable names makes it harder to read the code. I see this here in this forum a lot. I assume brevity of code is some kind of goal involved here.
Writing Code that is easily understandable with little effort is far more important to me. And of course readabilty / maintainibility on one side and efficient, clean and short code on the other side are both important and for both must be found a suitable balance. In real life, other people read my code to, or I myself after a long time again and I'd like to avoid that experience I had in the past: "Oh crap! What did I smoke when I wrote that code?"
As an awk example(Scrutinizers variant above is written much better than the first one): split(a,b,c) vs split(text,result,pattern)
This helps me even if I do not know the syntax of split, so I do not have to check the documentation for split() right away.
So I would like it to have code, I can easily understand. I like a solution more, which I may find in this forum even years after it was posted and I quickly can understand it.
Global Variables
(actually i decided to use introduces a new global variable in my fix) I think it's good design, to avoid global variables whenever possible(See http://wiki.c2.com/?GlobalVariablesAreBad). For this forum, a solution that does this without globals avoids copy-and-paste problems: "Huu, I take this function, and put it into my code, and I like it, if it just works." and bamm! Variable collision and it crashes or just does not work. Maybe this(not-working-code with side-effects) is a nice to have too in terms of: Make sure you really understand your code, before trying to run it! Do not just copy it! ...but for I appreciate the other way.
Hi All,
Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition.
#Body
function1()
{
if
then
echo "exist"
else
echo "not exist"
}
#if not exist in function1 my all other function will not proceed.... (4 Replies)
Not sure where the problem is. I can run the script without any issue using the following command.
. /opt/app/scripts/cdc_migration.sh
But it fails with the below error when I try it this way
/opt/app/scripts/cdc_migration.sh
/opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
hello,
I'm reading this thread, in which there is this code :awk '
function comb(v,i) {
for(i in A) {
delete A;
if(length(A))
comb((v?v"+":x)i)
else print v"+"i
A;
}
}
{ A }
END {
comb();
} ' infilebut I can't understand where does v come... (5 Replies)
Hi,
I need to filter my data based on items in column 23. Column 1 until column 23 are tab separated. This is how column 23 looks like:
PRIMARY=<0/1:504:499,5:.:.:.:0.01:1:15:.>
I want to extract lines if items 7 (separated by : ) in column 23 are more than 0.25 . In example above , item... (2 Replies)
Hello I have a very simple input file in which there are a list of numbers:
1
2
3
4
5
6
7
8
9
10
My actual dataset is about 200 lines long. I was wondering how to add different permutations of 3 numbers for all the numbers in the dataset. For example:
1+2+3; 3+5+7; 2+8+1; 9+3+4... (1 Reply)
OS=HP-UX ksh
The following works, except I want to include the <start> and <end> in the output.
awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log'
The following work in bash but not in ksh
sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
I have a file with the record of person:
cat > $TMP/record.txt
John Torres M Single 102353 Address
Mark Santos M Maried 103001 Address
Carla Maria F Maried 125653 Address
#!/bin/ksh
ManipulateID(){
...
return 0;
... #or
return 1;
}
cat $TMP/record.txt | awk 'BEGIN {printf... (4 Replies)
Hello Friends,
Is it possible to return an array from a user defined function in awk ?
example:
gawk '
BEGIN{}
{
catch_line = my_function(i)
print catch_line
print catch_line
print catch_line
}
function my_function(i)
{
print "echo"
line= "awk"
line= "gawk"... (2 Replies)