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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: How to print a text document to RTF document by thanos1983
in thread How to print a text document to RTF document by mdavies23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.