# Print an array as a table [closed]

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

# Requirements

• Standard loopholes are forbidden
• 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

# 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┃
┗━━━┛


## closed as unclear what you're asking by xnor, caird coinheringaahing, Mr. Xcoder, Wheat Wizard, Jo KingAug 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.

• 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"]) – Jonathan Allan Aug 11 at 16:24
• Can we use any normal box drawing characters instead of the heavy ones? – Adám Aug 11 at 16:34
• Can we use ┬ and ┴ when separating the columns? – Adám Aug 11 at 16:36
• @PurpleP there is no specification about justification – Micio Informatico Aug 11 at 16:36
• Maybe for next time, use the sandbox to get all the questions answered before posting? – Adám Aug 11 at 16:37

# 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

• Question over, go home folks. – Purple P Aug 11 at 16:57

# 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!

# 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!

# Charcoal, 62 bytes

Ｆ²⊞υ×━⌈ＥθＬ§κι⟦⪫┏┓⪫υ┳⟧Ｅθ⪫┃┃⪫Ｅι◨λＬ§υμ┃⪫┗┛⪫υ┻


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:

Ｆ²⊞υ×━⌈ＥθＬ§κι


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

⟦⪫┏┓⪫υ┳⟧


Embed the ━s in ┏┳┓.

Ｅθ⪫┃┃⪫Ｅι◨λＬ§υμ┃


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

⪫┗┛⪫υ┻


Embed the ━s in ┗┻┛.

# Python 3, 189172168173 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!