http://www.perlmonks.org?node_id=1046288


in reply to Re^4: create an xml file from column file
in thread create an xml file from column file

Another change in requirements? Wow. Full rewrite needed for this one.

use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new( { sep_char => "\t" } ); # assuming tab separ +ated input open my $words, "<", "words.txt" or die "Cannot open words.txt: $!\n"; # the following shows what's in words.txt; it is NOT words.txt itself! =head words.txt how o B-NP are o I-NP you o I-NP some o o really o B-GP =cut my $lasttype; my @text; while( my $row = $csv->getline( $words ) ) { my $text = $$row[0]; my $type = ( $$row[-1] =~ /\w-(\w+)/ ) ? $1 : ""; $lasttype = $type unless @text; # special treatment for first +row if( $type eq $lasttype ) { push @text, $text; } else { print '<key="type">'."$lasttype</key><text>@text</text +>\n" if $lasttype; $lasttype = $type; @text = ( $text ); } } # print what's left over when all input read print '<key="type">'."$lasttype</key><text>@text</text>\n" if $lasttyp +e; close $words;