use strict; use warnings; use XML::Simple; use Data::Dumper; # read __DATA__ file into $_ { $/ = undef; $_ = ; close DATA; } # use XML::Simple to read the table string my $ref = XMLin( $_ , NoAttr=>1 ); my $tr = $ref->{tr}; my @ARR; # flatten table structure into an array for my $row (keys @{$tr}){ my @X = values @{(values %{ $tr->[$row] })[0]}; push @ARR, @X; } # now we reformat the table my $COLUMNS = 2; my $counter = 0; my %S; for my $v (@ARR){ my $i=int($counter/$COLUMNS); my $j=int($counter % $COLUMNS); push @{$S{'tr'}->[$i]->{'td'}}, $v; ++$counter; } # We use XML::Simple to transform this stucture to an xml/html string my $xml = new XML::Simple (NoAttr=>1, RootName=>'table'); # convert Perl array ref into XML document my $new_table = $xml->XMLout(\%S); # now show the result print $new_table; __DATA__
Firstname Lastname Age
Jill Smith 50
Eve Jackson 94