# Flipping Dictionaries with Strings

**URL:** https://forum.kx.com/t/flipping-dictionaries-with-strings/11919
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [June 17, 2019, 2:21pm UTC](https://forum.kx.com/t/flipping-dictionaries-with-strings/11919 "2019-06-17T14:21:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Light\_of\_Heaven](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@Light\_of\_Heaven](https://forum.kx.com/u/Light_of_Heaven)
#### Post date: [June 17, 2019, 2:21pm UTC](https://forum.kx.com/t/flipping-dictionaries-with-strings/11919/1 "2019-06-17T14:21:00Z")

</div>

Hello,

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

> q)(!) . (`address`version`hex;(`1ASD7SJDK;2;(5;2;1;2;4;1;1)))  
> address| `1ASD7SJDK  
> version| 2  
> hex&nbsp; &nbsp; | 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 (!) . (`address`version`hex;(`1ASD7SJDK;2;(5;2;1;2;4;1;1))) address&nbsp; &nbsp;version hex
> 
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp;&nbsp;  
> 1ASD7SJDK 2&nbsp; &nbsp; &nbsp; &nbsp;1

With the dictionary below I get a length error.

> q)((!) . enlist@'(`imAQbie;“Helpmeplz”)),select txid,version:“i”$version from p  
> imAQbie | “Helpmeplz”  
> txid&nbsp; &nbsp;| “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 &nbsp; &nbsp; &nbsp; | 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 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15f

scriptPubKey| (`asm`hex`reqSigs`type`addresses!("OP_DUP OP_HASH160 fd8017c2ac5f56b554312cab973089596eddf93c OP_EQUALVERIFY OP_CHECKSIG";"76a914fd8017c2ac5f56b554312cab973089596eddf93c88ac";1f;"pubkeyhash";,"1Q7PQaWurNiBuzdjxwR7njkhtWB9e92wqW");`asm`hex`reqSigs`type`addresses!(“OP\_DUP OP\_HASH160 3b67fd818d1f830113d2bda1b9903be93ef8efc3 OP\_EQUALVERIFY OP\_CHECKSIG”;“76a9143b67fd818d1f830113d2bda1b9903be93ef8efc388ac”;1f;“pubkeyhash”;,“16R7SgtjfewCHfhEFniTpux6uU286Cuuo5”);`asm`hex`reqSigs`type`addresses!("OP_HASH160 d61fec2da4cdfbf582ab8351d481e89e338bb416 OP_EQUAL";"a914d61fec2da4cdfbf582ab8351d481e89e338bb41687";1f;"scripthash";,"3MDCmcbpB22PMovjTKkArze9UgeqVxr4Dm");`asm`hex`reqSigs`type`addresses!(“OP\_DUP OP\_HASH160 46d9b0aa75e69902dbebc9fd762a8547d5dcd489 OP\_EQUALVERIFY OP\_CHECKSIG”;"76..

txid| ,“ff62724b96b8424670e178283f63d80cebc7ccdc1922cf19e2ea2548268df8ff”

version | ,1i

size| ,696

vsize | ,696

locktime| ,0

&nbsp;

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 &nbsp; &nbsp; &nbsp; | 16

n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 16

scriptPubKey| 16

txid&nbsp; &nbsp; &nbsp; &nbsp; | 1

version &nbsp; &nbsp; | 1

size&nbsp; &nbsp; &nbsp; &nbsp; | 1

vsize &nbsp; &nbsp; &nbsp; | 1

locktime&nbsp; &nbsp; | 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.&nbsp;

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

Cheers

---

<div class="post-metadata">

### Author: ![TerryLynch1](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@TerryLynch1](https://forum.kx.com/u/TerryLynch1)
#### Post date: [June 18, 2019, 12:39pm UTC](https://forum.kx.com/t/flipping-dictionaries-with-strings/11919/2 "2019-06-18T12:39:00Z")

</div>

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)@\:/:`a`b`c`d!(1 2;`xyz;01:02;flip `q`r`s!(1 11;("foo";"bar");3 33))a| 7h 2b| -11h 1c| -17h 1d| 98h 2/failsq)(type;count)@\:/:`a`b`c`d!(1 2;"xyz";01:02;flip `q`r`s!(1 11;(“foo”;“bar”);3 33))a| 7h 2b| 10h 3c| -17h 1d| 98h 2/enlisting generally doesn’t help/failsq)(type;count)@:/:`a`b`c`d!(1 2;enlist “xyz”;01:02;flip `q`r`s!(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

---

<div class="post-metadata">

### Author: ![Light\_of\_Heaven](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@Light\_of\_Heaven](https://forum.kx.com/u/Light_of_Heaven)
#### Post date: [June 18, 2019, 12:51pm UTC](https://forum.kx.com/t/flipping-dictionaries-with-strings/11919/3 "2019-06-18T12:51:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![TerryLynch1](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@TerryLynch1](https://forum.kx.com/u/TerryLynch1)
#### Post date: [June 18, 2019, 4:04pm UTC](https://forum.kx.com/t/flipping-dictionaries-with-strings/11919/4 "2019-06-18T16:04:00Z")

</div>

No problem.&nbsp;

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