I am working on an Awk / Gawk script that parses a file, populating a multidimensional array for each row. The first column is a delimited row limited to a period, each value being a reference to an array key for the next level. The second column is the value
Here is an example of what the parsing looks like:
$ echo -e "personal.name.first\t= John\npersonal.name.last\t= Doe\npersonal.other.dob\t= 05/07/87\npersonal.contact.phone\t= 602123456\npersonal.contact.email\t= john.doe@idk\nemployment.jobs.1\t= Company One\nemployment.jobs.2\t= Company Two\nemployment.jobs.3\t= Company Three"
personal.name.first = John
personal.name.last = Doe
personal.other.dob = 05/07/87
personal.contact.phone = 602123456
personal.contact.email = john.doe@idk
employment.jobs.1 = Company One
employment.jobs.2 = Company Two
employment.jobs.3 = Company Three
That after the analysis, I expect it to have the same structure as:
data["personal"]["name"]["first"] = "John"
data["personal"]["name"]["last"] = "Doe"
data["personal"]["other"]["dob"] = "05/07/87"
data["personal"]["contact"]["phone"] = "602123456"
data["personal"]["contact"]["email"] = "john.doe@foo.com"
data["employment"]["jobs"]["1"] = Company One
data["employment"]["jobs"]["2"] = Company Two
data["employment"]["jobs"]["3"] = Company Three
The part I'm stuck on is dynamically populating keys when structuring a multidimensional array.
SO, , SUBSEP, , , , arr["foo", "bar"] = "baz" , arr["foo"]["bar"] = "baz". , , : arr["foo", "bar"] = "baz"; print length(arr["foo"]) 0 ()
SO, , , .
:
BEGIN {
x=SUBSEP
a="Red" x "Green" x "Blue"
b="Yellow" x "Cyan" x "Purple"
Colors[1][0] = ""
Colors[2][0] = ""
split(a, Colors[1], x)
split(b, Colors[2], x)
print Colors[2][3]
}
, , , , (EG: Red, Green ..) , .
, a_keys b_keys, . a b ?..
BEGIN {
x=SUBSEP
# How can I take these strings...
a_keys = "Red.Green.Blue"
b_keys = "Yellow.Cyan.Purple"
# .. And populate the array, just as this does:
a="Red" x "Green" x "Blue"
b="Yellow" x "Cyan" x "Purple"
Colors[1][0] = ""
Colors[2][0] = ""
split(a, Colors[1], x)
split(b, Colors[2], x)
print Colors[2][3]
}
, !