Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: create an xml file from column file

by hdb (Prior)
on Jul 25, 2013 at 07:58 UTC ( #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
Node Status?
node history
Node Type: note [id://1046288]
help
Chatterbox?
[ambrus]: Corion: ah, so you want a library that parses HTTP, and you want to do the IO yourself, and don't want a full AnyEvent wrapper.
[ambrus]: Corion: I think I parsed a HTTP header from a string with LWP once. You can definitely use that to create a HTTP message too. The problme is
[ambrus]: that if you do that, you'd have to find where each HTTP response ends, which is nontrivial if you want persistent connections (essential for performance if you have small requests).
[Corion]: ambrus: Yes, ideally an API that I feed the incoming data piece by piece and that I can ask "is that response done" and "what should I do next" and "please construct the appropriate redirect for me"

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2016-12-07 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (130 votes). Check out past polls.