1
\$\begingroup\$

Write a program that prints an associative array (key-value) to a table.

Requirements

  • Standard loopholes are forbidden
  • Please include a link to an online interpreter and the byte count in the answer
  • Use box drawing characters to draw the table.
  • You can write both a full program or a function.
  • Key or value can't contain multiline strings.
  • Value/key can't be an array/associative array ( example: ["a"=>["a"=>"b"]] or ["a"=>["a","b"]] )

Special cases

  • If the programming language lacks of associative array , you can use a list of pairs. Example: [[key1,value1],[key2,value2]]
  • You can insert horizontal lines between successive pairs.
  • An input array can't be empty

This is a code-golf, the answer with the shortest byte count wins!

Challenge

Your code must draw a vertical table like in the example from a associative array . The key must be at left and value must be at right and them needs to be separed,you can choose how separe them. Every key-value pair must be in another row. There must be only 2 columns for the keys and value.

Example

Array(PHP):

["a"=>"b","c"=>"d","d"=>"d"]

Output:

┏━━━┓
┃a┃b┃
┃c┃d┃
┃d┃d┃
┗━━━┛
\$\endgroup\$

closed as unclear what you're asking by xnor, caird coinheringaahing, Mr. Xcoder, Sriotchilism O'Zaic, Jo King Aug 11 at 21:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ If the language lacks associative arrays may we just use a list of pairs, [[k1,v1],[k2,v2],...], or a flat list [k1,v1,k2,v2,...]? \$\endgroup\$ – Jonathan Allan Aug 11 at 16:21
  • 1
    \$\begingroup\$ @JonathanAllan If the language lacks associative arrays you can use a list of pairs ( [[k1,v1],[k2,v2]] ) . \$\endgroup\$ – Micio Informatico Aug 11 at 16:22
  • 4
    \$\begingroup\$ Thanks, please edit the post accordingly (we prefer not to have spec in comments). Also what are the rules regarding justification? (e.g ["abba"=>"1","b"=>"20"]) \$\endgroup\$ – Jonathan Allan Aug 11 at 16:24
  • \$\begingroup\$ Must it be a full program or can we write a function instead? \$\endgroup\$ – Purple P Aug 11 at 16:31
  • 3
    \$\begingroup\$ Maybe for next time, use the sandbox to get all the questions answered before posting? \$\endgroup\$ – Adám Aug 11 at 16:37
8
\$\begingroup\$

TryAPL, 1 byteSBCS

Anonymous tacit prefix function. As TryAPL has no associative arrays, this takes a list of pairs as argument.

Try APL!

 mix the list of pairs into a table

\$\endgroup\$
  • 6
    \$\begingroup\$ Question over, go home folks. \$\endgroup\$ – Purple P Aug 11 at 16:57
3
\$\begingroup\$

J, 1 byte

>

So… to take strings as input they need to be in boxes already, and to make them pairs I think they need to be put into a box again so that they can be treated separately. I'm not too sure about the second part.

Anyway, unboxing the whole thing gives the desired form.

I get a sense that this could be a 0 byte answer if we twist the format enough (or maybe less, since a 2d array of boxes seems to be the proper input format instead of my weird format), but I don't understand boxing all that well.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 563 bytes

<?php
    $t = ["a"=>"b","c"=>"d"];
    $k = $v = $i = $j = 0;
    foreach ($t as $a => $b){
        $k = (strlen($a) > $k ? strlen($a) : $k);
        $v = (strlen($b) > $v ? strlen($b) : $v);
    }
    echo "┏"; for(; $i++ < $k + $v + 3;) echo "━";
    echo "┓\n";
    foreach ($t as $a => $b){
        echo "|{$a} ";
        for($i = 0; $i < $k - strlen($a); $i++) echo " ";
        echo "| {$b}";
        for($i = 0; $i < $v - strlen($b); $i++) echo " ";
        echo "|\n";
    }
    echo "┗"; for(; $j++ < $k + $v + 3;) echo "━";
    echo "┛";
?>

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 62 bytes

F²⊞υ×━⌈EθL§κι⟦⪫┏┓⪫υ┳⟧Eθ⪫┃┃⪫Eι◨λL§υμ┃⪫┗┛⪫υ┻

Try it online! Link is to verbose version of code. Charcoal's code page doesn't include box-drawing characters so they cost 3 bytes but the code uses them instead of their three-byte encoding because it looks nicer. Takes input as an array of tuples as Charcoal doesn't have an iterable dictionary. Explanation:

F²⊞υ×━⌈EθL§κι

Find the length of the longest key and value and repeat according to that length.

⟦⪫┏┓⪫υ┳⟧

Embed the s in ┏┳┓.

Eθ⪫┃┃⪫Eι◨λL§υμ┃

For each tuple, pad the strings to the length of the s and wrap them with s.

⪫┗┛⪫υ┻

Embed the s in ┗┻┛.

\$\endgroup\$
0
\$\begingroup\$

Python 3, 189 172 168 173 169 bytes

def f(d):t=d.items();a,b=map(lambda*l:max(map(len,l)),*t);c='─'*a,'─'*b;print("┌%s┬%s┐"%c,*[f"│{k:{a}}│{v:{b}}│"for k,v in t],"└%s┴%s┘"%c,sep="\n")

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.