0

I am working on question 4 of adventofcode. I have a list of strings called "q4" where there are 3 lines (just simple data for now) and each line has keys & values, such as: passport ID being 662406624, or birth year being 1947, etc.

show q4
"eyr:2024 pid:662406624 hcl:#cfa07d byr:1947 iyr:2015 ecl:amb hgt:150cm"
"iyr:2013 byr:1997 hgt:182cm hcl:#ceb3a1 eyr:2027 ecl:gry cid:102 pid:018128"
"hgt:61in iyr:2014 pid:916315544 hcl:#733820 ecl:oth"

I created a function to grab the value for a given key

get_field_value: {[field; pp_str] pp_fields: " " vs pp_str; pid_field:  pp_fields[where like[pp_fields; field,":*"]]; start_i: (pid_field[0] ss ":")[0] + 1; end_i: count pid_field[0]; indices: start_i + til (end_i - start_i); pid_field[0][indices]}

fields: ("eyr"; "pid"; "hcl"; "byr"; "iyr"; "ecl"; "hgt")

With the help from this other thread, I could get the values for a given list of keys: kdb/q: How to apply a string manipulation function to a vector of strings to output a vector of strings?

get_field_value[; q4[0]] each fields        / Iterates through each field
"2024"
"662406624"
"#cfa07d"
"1947"
"2015"
"amb"
"150cm"

But now how do I do this for each line in my text file (each of the 3 strings in "q4")? In Python or C++ logic, basically I want to do a nested for-loop, the outer loop to iterate through each of the strings, and then within that, for each string, the inner loop grabs the value for each of the keys (fields):

/ Attempt 1 - Fail
get_field_value[each fields ; each q4]

/ Attempt 2 - Fail
each[get_field_value[; each q4]] fields

/ Attempt 3 - Fail
get_field_value[; each q4] each fields

How do I do this? Thanks!

JZL
  • 53
  • 9
  • This could possibly have just been a followup question to https://stackoverflow.com/questions/65441151/kdb-q-how-to-apply-a-string-manipulation-function-to-a-vector-of-strings-to-out – terrylynch Dec 28 '20 at 16:05
  • Apologies, I am new to stackoverflow. How do I follow-up question? Do I just comment in the old thread, in response to your answer? Or should I write it under "answer my own question?" – JZL Dec 28 '20 at 16:23
  • You can ask the follow up question in the comments of peoples responses – terrylynch Dec 28 '20 at 18:15
  • Thanks I just posted a comment in response to you in that thread. But it doesn't seem to support paragraph separation nor code formatting – JZL Dec 28 '20 at 19:15

0 Answers0