in reply to Formatting

Sometimes it's best to create a table and lookup the formatting as you iterate through the file.
#!/bin/perl5 use strict; use warnings; my $tag = { K => { open => '%', close => '', block => 1, block_open => '<p>', block_close => '</p>' }, R => { open => '<li>', close => '</li>', block => 0 }, T => { open => '<td>', close => '</td>', block => 0 } }; my $block_flag = 0; my $line_out = ''; my $output = ''; while (my $line_in = <DATA>) { chomp($line_in); my ( $style, $content ) = $line_in =~ /^(\w)\s+(.*)$/; $line_out = join( '', $tag->{ $style }->{ open }, $content, $tag->{ $style }->{ close } ); if ( $tag->{ $style }->{ block } and ! $block_flag ){ $output = join( '', $output, "\n", $tag->{ $style }->{ block_open }, $line_out ); $block_flag = $style; } elsif ( ! $tag->{ $style }->{ block } and $block_flag ){ $output = join( '', $output, $tag->{ $block_flag }->{ block_close }, "\n", $line_out, "\n" ); $block_flag = ''; } else{ $output = join( '', $output, $line_out ); } $line_out = ''; } open OUT, ">", "output.txt" or die; print OUT $output; close OUT; __DATA__ R Whatever K Perl K Monks K Is K Cool T Another style R Whatever
Produces...
<li>Whatever</li> <p>%Perl%Monks%Is%Cool</p> <td>Another style</td> <li>Whatever</li>
A bit extreme, not elegant. But flexible and easily changed if formats change or are added.
Hope this helps
wfsp

Replies are listed 'Best First'.
Re^2: Formatting
by mrxg4 (Initiate) on Jul 10, 2004 at 13:13 UTC
    Hey,
    First of all, thanks to all who replied!

    John, I like you sample script because, like you said, its flexible. I've tried it out and it works all well except one thing, after a </li>, when it has to start with another <li>, it does it all in the same line. I've tried adding a \n to the close string, but it isn't working, its getting outputted instead of interpreted.

    Like I said before, I'm new in Perl and this is probably really easy to fix.

    Thanks again! I really appreciate it :)
    Marcos
      Glad you liked it. I've put another condition in the if block:
      if ( $tag->{ $style }->{ block } and ! $block_flag ){ $output = join( '', $output, $tag->{ $style }->{ block_open }, $line_out ); $block_flag = $style; } elsif ( ! $tag->{ $style }->{ block } and $block_flag ){ $output = join( '', $output, $tag->{ $block_flag }->{ block_close }, "\n", $line_out, "\n" ); $block_flag = ''; } elsif ( ! $tag->{ $style }->{ block } ){ $output = join( '', $output, $line_out, "\n" ); } else{ $output = join( '', $output, $line_out ); }
        Thanks again!

        Just got 2 questions that have arisen and should be easy to do now that you have kindly provided me with this script.

        First, the simplest, how do I add the '#' as one of the first characters to check? I have tried adding another one of those blocks, but as you know Perl's comment character is #. Adding "\#" didn't help either. How can I express this in a way that will work without Perl interpreting it as a comment?

        And lastly, how can I make the "K" statements be separated as "Perl,%Monks,%Is,%Cool"? I've experimented with adding a ",". For example:
        my $tag = { K => { open => ',%',
        close => '',
        block => 1,
        block_open => '<p>',
        block_close => '</p>'

        But as you probably realized, this has a nasty habit of adding an extra "," even to the first statement, as in ,%Perl,%Monks,%Is,%Cool .

        I want you to know that I really appreciate the help you have provided so far, since I'm very close to having this finished and saving me tons of future hours :)

        Best Regards,
        Marcos