in reply to Parsing by indentation
Something like this ?
#!/usr/bin/perl # https://perlmonks.org/?node_id=1224600 use strict; use warnings; use Data::Dump 'dd'; my $data = <<'END'; interface XYZ given param1 -> child of "interface XYZ" given param2 -> child of "interface XYZ" given param2.1 -> child of "given param2" given param2.1.1 -> child of "given param2.1" given param2.1.2 -> child of "given param2.1" given param2.2 -> child of "given param2" given param3 -> child of "interface XYZ" given param4 -> child of "interface XYZ" interface SECOND given param5 -> child of "interface SECOND" END my $struct = buildstruct($data); dd $struct; sub buildstruct { my $block = shift; my @answers; while( $block =~ /^( *)(.*)\n((?:\1 +.*\n)*)/gm ) { my ($head, $rest) = ($2, $3); $head =~ s/ ->.*//; push @answers, $rest ? { $head => buildstruct($rest) } : $head; } \@answers; }
Outputs:
[ { "interface XYZ" => [ "given param1", { "given param2" => [ { "given param2.1" => ["given param2.1.1", "given param2.1.2 +"] }, "given param2.2", ], }, "given param3", "given param4", ], }, { "interface SECOND" => ["given param5"] }, ]
Or do I completely misunderstand what you're asking for ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing by indentation
by llarochelle (Beadle) on Oct 24, 2018 at 20:10 UTC | |
by tybalt89 (Monsignor) on Oct 24, 2018 at 20:49 UTC | |
by llarochelle (Beadle) on Oct 25, 2018 at 02:14 UTC | |
by llarochelle (Beadle) on Oct 25, 2018 at 14:13 UTC | |
by hippo (Archbishop) on Oct 25, 2018 at 15:16 UTC | |
by llarochelle (Beadle) on Oct 26, 2018 at 13:41 UTC | |
| |
by AnomalousMonk (Archbishop) on Oct 25, 2018 at 17:02 UTC | |
by LanX (Saint) on Oct 27, 2018 at 16:21 UTC | |
by llarochelle (Beadle) on Oct 29, 2018 at 12:56 UTC |