use strict; use warnings; use Data::Dump qw/pp dd/; my @parse; my @path; my $last_level=0; $path[$last_level] = \@parse; while (my $line = ) { # pp my($white,$key,undef,$content) = $line =~ /^ (\s*) # indent (.*?) # key ( \s*->\s* # ignore arrow (.*) )? # optional group $/x; my $level = length($white) / 2; # pp [$white, $key,$level,$last_level]; die "indent-level $level too big (last level was $last_level)!" if $level > $last_level+1; my @children; push @{$path[$level]}, # { # $key => { # children => \@children, # # level => $level, # # content => $content, # } # }; { $key => \@children # terse output }; $path[$level+1] = \@children; $last_level = $level; } warn "Output: " , pp \@parse; __DATA__ interface XYZ given param1 -> child of "interface XYZ" given param2 -> child of "interface XYZ" given param2.1 -> child of "given param2" given param2.2 -> child of "given param2" given param2.2.1 -> child of "given param2.2" given param3 -> child of "interface XYZ" #### Output: [ { "interface XYZ" => [ { "given param1" => [] }, { "given param2" => [ { "given param2.1" => [] }, { "given param2.2" => [{ "given param2.2.1" => [] }] }, ], }, { "given param3" => [] }, ], }, ] at d:/tmp/parse_indent.pl line 55, line 7.