Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dump() does not display literal array #5

Open
greencardamom opened this issue May 11, 2018 · 2 comments
Open

dump() does not display literal array #5

greencardamom opened this issue May 11, 2018 · 2 comments

Comments

@greencardamom
Copy link

greencardamom commented May 11, 2018

A version of dump() that displays the literal array:

function dump(array, prefix, i,j,c,a,k,s,sep) { 

  for(i in array) {
    j = i
    c = split(i, a, SUBSEP, sep)
    for(k = 1; k <= length(sep); k++) {
      gsub(/\\/, "\\", sep[k])
      gsub(/\//, "\\/", sep[k])
      gsub(/\t/, "\\t", sep[k])
      gsub(/\n/, "\\n", sep[k])
      gsub(/\r/, "\\r", sep[k])
      gsub(/\b/, "\\b", sep[k])
      gsub(/\f/, "\\f", sep[k])
      gsub(SUBSEP, ",", sep[k])
      gsub(/[\001-\037]/, "¿", sep[k]) # TODO: convert to octal?
    }
    s = ""
    for(k = 1; k <= c; k++) 
      s = s "\"" a[k] "\"" sep[k]
    printf "%s[%s]=%s\n", prefix, s, array[i]
  }
}

Given the sample json file (from issue #4) and this code:

query_json(readfile("test.json"), json)       
dump(json, "json")

Original dump():

json[results,0]=1
json[results,1,archived_snapshots,closest,available]=1
json[results,1,archived_snapshots,closest,status]=200
json[results,1,archived_snapshots,closest,timestamp]=20061231083247
json[results,1,archived_snapshots,closest,url]=http://web.archive.org/web/20061231083247/http://www.nytimes.com:80/
json[results,1,timestamp]=20070101
json[results,1,url]=http://nytimes.com

..new dump()

json["results","0"]=1
json["results","1","archived_snapshots","closest","available"]=1
json["results","1","archived_snapshots","closest","status"]=200
json["results","1","archived_snapshots","closest","timestamp"]=20061231083247
json["results","1","archived_snapshots","closest","url"]=http://web.archive.org/web/20061231083247/http://www.nytimes.com:80/
json["results","1","timestamp"]=20070101
json["results","1","url"]=http://nytimes.com

Copy and paste a line:

print json["results","1","url"]

..producing:

http://nytimes.com

Print all "url" records:

for(i = 1; i <= (("results","0") in json); i++) {
  if( ("results",i,"url") in json)  
    print json["results",i,"url"]
}

Above code is MIT license

@dubiousjim
Copy link
Owner

Thanks! I won't be able to get to it right away, but this will be useful to work in and I appreciate the contribution.

@greencardamom
Copy link
Author

Realized the code was overly complicated because Gawk has built-in magic to handle commas in multi-dimensional arrays -- so there isn't any good reason to change the value of SUBSEP. I've updated the original post with new code and output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants