CSV to HTML translation: Difference between revisions

→‎{{header|Perl 6}}: restrucured the code and removed the optional part, since it didn't work anyway.
(→‎{{header|Perl 6}}: added some comments)
(→‎{{header|Perl 6}}: restrucured the code and removed the optional part, since it didn't work anyway.)
Line 1,923:
 
=={{header|Perl 6}}==
 
I'm not sure why this looks so messy, tried my best with comments, but this very funtional approach seems very unreadable.
 
 
Line 1,934 ⟶ 1,932:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!¦;
 
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above
# my $str = $*IN.slurp;
 
my &escape = *.trans([ <& < \>>] => [<&amp; &lt; &gt;> ]); # a function with one argument that escapes the entities
my &tag = {"<$^ttag>"~$^swhat~"</$^ttag>"}; # a function that takes a string and a tagname, and tags it
 
 
printf #printf statement
'<!DOCTYPE html>
<html>
Line 1,945 ⟶ 1,947:
%s
</table></body></html>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
', [~] (escape($str).split(/\n/).map(-> $l { # concatenation reduction, # escape the string, thenand split it at \n, then anonymous functionnewline
state $first //= 0; # state variable for considering if it is the first line (no ff implemented, yet)
==> #tagmap it-> with$line {tag 'tr', split at ',' ==> tag those as td if# $first++feed (whichthat meansinto thea firstmap, timethat itmap function will betag falseas 'tr, thenand true)has alsean asargument thcalled $line
tag( ([~] $lline.split(/','/).map({tag($^a,\ # split $first++line ??at 'td,' !!'th' )})),'tr')});
# that / at the end is just an unspace, you can omit it, but then you have to delete
# all whitespace and comments between split(…) and .map
.map({tag 'td', $^cell})) }); # map those cells as td
</lang>
 
38

edits