Srini V
Srini V

Reputation: 11355

AWK: How to load a file into array and store the final results into another array

I have an input file with the below content

child, parent, val
1  , 0  , a
2  , 1  , b
3  , 1  , c
4  , 2  , d
5  , 2  , e

I need to store them in an array named data_array by directly reading from the file without the header. Something like this

BEGIN {
while (getline < "input")
{
split($0,ft,",");
child=ft[1];
parent=ft[2];
value=ft[3];
#need help here in assigning two values into the array
data_array[child]=parent,value;
}
close("input");
}

The result_array holds the parent to child relationship with ordering.

result_array[parent]="all children separated by comma"

For example, parent 0 has one child called 1. Parent 1 has two children called 2, and 3. The order of 2 and 3 are determined by alphabetically sorting the corresponding values. Since the sorting of values results in b followed by c the array element should have 2,3. There could be any number of children. Childless nodes must be written with blank content. These results must go into the final array in the following format.

Need help on this part to convert the data_array into the result_array

result_array["0"] = "1"
result_array["1"] = "2,3"
result_array["2"] = "4,5"
result_array["3"] = ""
result_array["4"] = ""
result_array["5"] = ""

Please shout if this is unclear.

Upvotes: 1

Views: 3079

Answers (1)

Ed Morton
Ed Morton

Reputation: 203254

With GNU awk for true multi-dimensional arrays and sorted_in:

$ cat tst.awk
BEGIN { FS=" *, *" }
NR==1 { for (i=1;i<=NF;i++) f[$i]=i; next }
{ parentsChildren2Vals[$(f["parent"])][$(f["child"])] = $(f["val"]) }
END {
    for (parent in parentsChildren2Vals) {
        PROCINFO["sorted_in"] = "@val_str_asc"
        for (child in parentsChildren2Vals[parent]) {
            parents2children[parent] = (parent in parents2children ?
                        parents2children[parent] "," : "") child
            children[child]
        }
    }

    for (child in children) {
        parents2children[child]
    }

    PROCINFO["sorted_in"] = "@ind_num_asc"
    for (parent in parents2children) {
        printf "parents2children[\"%s\"] = \"%s\"\n", parent, parents2children[parent]
    }
}

$ awk -f tst.awk file
parents2children["0"] = "1"
parents2children["1"] = "2,3"
parents2children["2"] = "4,5"
parents2children["3"] = ""
parents2children["4"] = ""
parents2children["5"] = ""

Upvotes: 3

Related Questions