Tree datastructures: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: added Perl6 example) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Minor stylistic twiddles) |
||
Line 200: | Line 200: | ||
#`( |
#`( |
||
If, on the other hand, we want perform more complex transformations |
If, on the other hand, we want perform more complex transformations; better to |
||
load it into a native data structure |
load it into a native data structure which will then allow us to manipulate it |
||
however we like. |
however we like. |
||
) |
) |
||
Line 213: | Line 213: | ||
for $tree.lines -> $line { |
for $tree.lines -> $line { |
||
$line ~~ /^^ ($($level))* /; |
$line ~~ /^^ ($($level))* /; |
||
my $this; |
|||
$forest ~= do { |
|||
given ($this = +$0) cmp $last { |
|||
when More { "\['{$line.trim}', " } |
|||
when Same { "'{$line.trim}', " } |
|||
when Less { "{']' x $last - $this}, '{$line.trim}', " } |
|||
} |
|||
} |
} |
||
$last = $this; |
|||
} |
} |
||
} |
} |
||
$forest ~= |
$forest ~= ']' x 1 + $last; |
||
use MONKEY-SEE-NO-EVAL; |
use MONKEY-SEE-NO-EVAL; |
||
$forest.=EVAL; |
$forest.=EVAL; |
||
Line 236: | Line 240: | ||
use YAML; |
use YAML; |
||
say "\nYAML:\n", $forest.&dump; |
say "\nYAML:\n", $forest.&dump; |
||
⚫ | |||
} |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>RosettaCode |
<pre>RosettaCode |
||
Line 285: | Line 288: | ||
ok 1 - Round-trip equals original |
ok 1 - Round-trip equals original |
||
Native data structure: |
Native data structure: |
||
$["RosettaCode", ["encourages", ["code", ["diversity", "comparison"]], "discourages", ["golfing", "trolling", "emphasising execution speed"]], "code-golf.io", ["encourages", ["golfing"], "discourages", ["comparison"]]] |
$["RosettaCode", ["encourages", ["code", ["diversity", "comparison"]], "discourages", ["golfing", "trolling", "emphasising execution speed"]], "code-golf.io", ["encourages", ["golfing"], "discourages", ["comparison"]]] |