Tree datastructures: Difference between revisions

Content added Content deleted
m (→‎{{header|zkl}}: added Perl6 example)
m (→‎{{header|Perl 6}}: Minor stylistic twiddles)
Line 200: Line 200:


#`(
#`(
If, on the other hand, we want perform more complex transformations, better to
If, on the other hand, we want perform more complex transformations; better to
load it into a native data structure, which will then allow us to manipulate it
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))* /;
given (my $this = +$0) cmp $last {
my $this;
when More { $forest ~= "\['{$line.trim}', "; $last = $this }
$forest ~= do {
when Same { $forest ~= "'{$line.trim}', " }
given ($this = +$0) cmp $last {
when Less { $forest ~= "{']' x $last - $this}, '{$line.trim}', "; $last = $this }
when More { "\['{$line.trim}', " }
when Same { "'{$line.trim}', " }
when Less { "{']' x $last - $this}, '{$line.trim}', " }
}
}
}
$last = $this;
}
}
}
}


$forest ~= "{']' x 1 + $last}";
$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;
}</lang>
}
</lang>
{{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"]]]