#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my @lines = ; chomp @lines; my @list = ([q{div}, {id => q{article}}]); my (@current_list); my $depth = 0; for my $line (@lines){ my ($tag, $txt) = $line =~ /^;([^;]+);(.*)/; if ($tag eq q{greybox_start}){ push @{$list[$depth]}, @current_list; push @{$list[$depth]}, [q{div}, {class => q{greybox}}]; $depth++; @current_list = (); next; } elsif ($tag eq q{greybox_end}){ $depth--; push @{$list[$depth]}, @current_list; @current_list = (); next; } push @current_list, [$tag, $txt]; } push @{$list[$depth]}, @current_list; print Dumper(\@list), __DATA__ ;p;one ;greybox_start; ;h2;two ;greybox_end; ;p;three