in reply to How to print a text document to RTF document
Hello mdavies23
I thought that I provided you with a solution on how to write to a RTF file at Re: How to tokenize a RTF file and print it to another file (Update with solution).
But never the less let me try again. I would suggest some modifications on your code. For example you are calling use use RTF::Parser; but you do not use it. Then you are opening a write file handle open my $output, ">", $outfile; for no reason. The module RTF::Writer handles that for you, you do not need to use this (remove it). Last point you open a file but you do not use close die or warn. I wrote that also on my previous answer it is very important it help you know why your code might fail.
I put together a working example based on the sample.rtf document that I posted on my previous answer. I can not fully test your code because you have not provide us a sample of input data and you are comparing numbers probably. So in my case I am getting error such as Useless use of numeric comparison (<=>) in void context at test.pl line 35, <DATA> line 183. Argument "La" isn't numeric in numeric comparison (<=>) at test.pl line 35.. But it should help you get going the following part of working code.
#!/usr/bin/perl use say; use strict; use warnings; use RTF::Writer; use Data::Dumper; use RTF::TEXT::Converter; die "usage: $0 input output\n" unless @ARGV == 2; my $infile = shift; my $outfile = shift; open my $rtf_data, "<", $infile or die "Could not open the ".$infile.": $!"; my $output_str; my $object = RTF::TEXT::Converter->new( output => \$output_str ); $object->parse_stream( $rtf_data ); close $rtf_data or warn "Could not close ".$infile.": $!"; chomp $output_str; # say $output_str; my @un = split("\n", $output_str); my $rtf = RTF::Writer->new_to_file($outfile); my @sorted = sort { my @fields_a = split ' ' , $a; my @fields_b = split ' ', $b; chomp($a, $b); $fields_a[0] <=> $fields_b[0]; if($fields_a[1] || $fields_b[1] eq 'ERROR'){ $rtf->prolog(); $rtf->print($fields_a[1]); } } @un; print Dumper \@sorted; $rtf->close;
If you have any further problems let us know.
Hope this helps, BR.
|
|---|