Flipping Dictionaries with Strings

Hello,

I’m having difficulty flipping dictionaries that have strings. I always get a length error. Here is a simple example.

q)(!) . (addressversionhex;(1ASD7SJDK;2;(5;2;1;2;4;1;1)))
address| `1ASD7SJDK
version| 2
hex    | 5 2 1 2 4 1 1

When I flip this dictionary with unequal length, the columns address and version automatically conform. I like this!

q)flip (!) . (addressversionhex;(1ASD7SJDK;2;(5;2;1;2;4;1;1)))
address   version hex

1ASD7SJDK 2       5  
1ASD7SJDK 2       2  
1ASD7SJDK 2       1  
1ASD7SJDK 2       2  
1ASD7SJDK 2       4  
1ASD7SJDK 2       1  
1ASD7SJDK 2       1

With the dictionary below I get a length error.

q)((!) . enlist@'(`imAQbie;“Helpmeplz”)),select txid,version:“i”$version from p
imAQbie | “Helpmeplz”
txid   | “3dfcdffbc53f04f9f7139942e786c1ec26694ac973822e4e919f71580c48e3a6”
version| 1i

Here is another example (the one I’m actually working on). Take a look at this dictionary.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff; background-color: #2b66c9}span.s1 {font-variant-ligatures: no-common-ligatures}

q)(flip (first l[`vout])),exec txid,version:“i”$version,size:“j”$size,vsize:“j”$vsize,locktime:“j”$locktime from l

value       | 7.664098 0.005165 0.00073 0.017 0.002205 0.000281 0.006913 0.079418 0.000905 0.001047 0.000817 0.0013 0.003996 6e-05 0.00021 0.002034

n           | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15f

scriptPubKey| (asmhexreqSigstypeaddresses!("OP_DUP OP_HASH160 fd8017c2ac5f56b554312cab973089596eddf93c OP_EQUALVERIFY OP_CHECKSIG";"76a914fd8017c2ac5f56b554312cab973089596eddf93c88ac";1f;"pubkeyhash";,"1Q7PQaWurNiBuzdjxwR7njkhtWB9e92wqW");asmhexreqSigstypeaddresses!(“OP_DUP OP_HASH160 3b67fd818d1f830113d2bda1b9903be93ef8efc3 OP_EQUALVERIFY OP_CHECKSIG”;“76a9143b67fd818d1f830113d2bda1b9903be93ef8efc388ac”;1f;“pubkeyhash”;,“16R7SgtjfewCHfhEFniTpux6uU286Cuuo5”);asmhexreqSigstypeaddresses!("OP_HASH160 d61fec2da4cdfbf582ab8351d481e89e338bb416 OP_EQUAL";"a914d61fec2da4cdfbf582ab8351d481e89e338bb41687";1f;"scripthash";,"3MDCmcbpB22PMovjTKkArze9UgeqVxr4Dm");asmhexreqSigstypeaddresses!(“OP_DUP OP_HASH160 46d9b0aa75e69902dbebc9fd762a8547d5dcd489 OP_EQUALVERIFY OP_CHECKSIG”;"76..

txid | ,“ff62724b96b8424670e178283f63d80cebc7ccdc1922cf19e2ea2548268df8ff”

version | ,1i

size | ,696

vsize | ,696

locktime | ,0

 

Here is a count.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff; background-color: #2b66c9}span.s1 {font-variant-ligatures: no-common-ligatures}

value       | 16

n           | 16

scriptPubKey| 16

txid        | 1

version     | 1

size        | 1

vsize       | 1

locktime    | 1

When I flip it I want Locktime, txid, version, ize, and vsize to be duplicated 16 times so that I don’t get a length error. I also don’t know why they’ve enlisted themselves (see highlighted yellow above).

Sorry for this long question, and thanks for your help. 

Bonus points if you realised I have a dictionary in a dictionary (scriptPubKey) and if you have an elegant way to resolve this.

Cheers

Flip is somewhat tolerant but not this tolerant! To the best of my knowledge flip will conform if the non-atomic (count>1) items all have the same count while the atomic items (count=1) have type not equal 0h, 98h or 99h

/worksq)(type;count)@\:/:abcd!(1 2;xyz;01:02;flip qrs!(1 11;("foo";"bar");3 33))a| 7h 2b| -11h 1c| -17h 1d| 98h 2/failsq)(type;count)@\:/:abcd!(1 2;"xyz";01:02;flip qrs!(1 11;(“foo”;“bar”);3 33))a| 7h 2b| 10h 3c| -17h 1d| 98h 2/enlisting generally doesn’t help/failsq)(type;count)@:/:abcd!(1 2;enlist “xyz”;01:02;flip qrs!(1 11;("foo";"bar");3 33))a| 7h 2b| 0h 1c| -17h 1d| 98h 2

You’ll likely have to rethink how you’re storing the data - dictionaries with different lengths and types (that you intend to flip) is going to be tricky.

Terry

This is an amazing answer, exactly what I was looking for.

I just realised I can highlight code syntax, so double thanks for putting up with the horrible formatted code in this post.

Cheers.

No problem. 

But slight correction - in the count=1 case it seems the types can be <0h or >=100h