#`«
From https://colabti.org/irclogger/irclogger_log/raku?date=2020-01-04#l173
BTW, parenthesis don't create lists. In Raku, lists are created with
commas and semi-colons so I did away with the parentheses.
All of this could be written more idiomatically but it's a starting point.
»
# These are my arrays
my @name = "Renuka", "Dipendra", "Larissa", "Ram", "Hari", "Roshan";
my @age = 20, 15, 45, 30, 40, 23, 14;
my @address = "Chitwan", "Dhangadhi", "Budhanilkantha", "Bharatpur", "Butwal", "Bhairahawa";
my @streetadd = "123Street", "Ward7", "Thamel", "Krishna7Street", "Milan234", "Buddha23";
constant $pad = 'NA';
constant $extra-space = 3;
constant $separator = '—';
constant $formatting = "%-*s %-*s %-*s %-*s";
# Pad the shorter lists.
my Int $longest-list = (@name, @age, @address, @streetadd)>>.elems.max;
for @name, @age, @address, @streetadd -> $list {
$list.push: |($pad xx ($longest-list - $list.elems));
}
# get length of the longest string from each array.
my ($name-len, $age-len, $address-len, $street-len) =
(@name, @age, @address, @streetadd)
.map(-> $arr { $arr>>.chars.max });
# increment spacing between columns
given $extra-space {
$name-len += $_;
$age-len += $_;
$address-len += $_;
$street-len += $_;
}
# this length is for the table's lines
my Int $total-len = [+] $name-len, $age-len, $address-len, $street-len;
for [Z] @name, @age, @address, @streetadd -> ($na, $ag, $ad, $st) {
FIRST {
say repeat-char $separator, $total-len;
say get-row $formatting,
$name-len, 'name',
$age-len, 'age',
$address-len, 'address',
$street-len, 'streetadd'
;
say repeat-char $separator, $total-len;
}
say get-row $formatting,
$name-len, $na,
$age-len, $ag,
$address-len, $ad,
$street-len, $st
;
LAST { say repeat-char $separator, $total-len }
}
sub get-row( Str $formatting, *@cells --> Str ) {
sprintf $formatting, |@cells;
}
sub repeat-char( Str $char, Int $times --> Str ) {
$char x $times
}
=begin comment
---------------------------------------------
name age address streetadd
---------------------------------------------
Renuka 20 Chitwan 123Street
Dipendra 15 Dhangadhi Ward7
Larissa 45 Budhanilkantha Thamel
Ram 30 Bharatpur Krishna7Street
Hari 40 Butwal Milan234
Roshan 23 Bhairahawa Buddha23
NA 14 NA NA
---------------------------------------------
=end comment