awkenough icon indicating copy to clipboard operation
awkenough copied to clipboard

dump() does not display literal array

Open greencardamom opened this issue 6 years ago • 2 comments

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

greencardamom avatar May 11 '18 14:05 greencardamom

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.

dubiousjim avatar May 11 '18 16:05 dubiousjim

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.

greencardamom avatar May 12 '18 15:05 greencardamom