Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: create an xml file from column file

by hdb (Monsignor)
on Jul 25, 2013 at 07:58 UTC ( [id://1046288]=note: print w/replies, xml ) Need Help??


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;

Replies are listed 'Best First'.
Re^6: create an xml file from column file
by lakssreedhar (Acolyte) on Jul 25, 2013 at 09:19 UTC

    is it possible to do without CSV .The code i wrote is below.But the required output is not coming

    $out=" "; my @text; while(<>) { chomp; @arr=split(/\s+/,$_); push @r,$arr[0]; push @e,join' ',($arr[0],$arr[2]); } print '<text>'; print join ' ',@r; print '</text>'; print"\n"; for my $a (@e) { @q=split(/\s/,$a); if($q[1]=~/^B-/) { $p=$'; $m=$q[0] if( $q[1] =~/^B-/ or $q[1]="o"); $out.="<annotation>\n<key=type>$p</type>\n<text>$m</text>\n</annotatio +n>\n"; } } print"$out\n";

      At your own risk, splitting on tab:

      use strict; use warnings; open my $words, "<", "words.txt" or die "Cannot open words.txt: $!\n"; my $lasttype; my @text; while( <$words> ) { chomp; my @row = split /\t/; 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;

        It is working but this error is popping up Use of uninitialized value in pattern match (m//) at f.pl line 14, <$words> line 18.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1046288]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-24 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found