Hello Perl Monks,
I have recently developed (thanks to the help of a Perl Monk) a program that reads a whole file, and depending on the first character of every line, it formats the content appropriately.

For instance, lets say that the file reads:
C 100
K Perl
K Monks
K Is
K Cool
F 1234
R Whatever
R More
R BlaBla

This will equal (whatever comes after C in the same line is ignored):
<c>
<p>%Perl,%Monks,%Is,%Cool</p>
<t>
<setflag param="1234"/>
<r>
<li>Whatever</li>
<li>More</li>
<li>BlaBla</li>
</r>
</t>
</c>

This is very simple and has been implemented already (see the code at the end), the problem comes when the block begins with an I (capital 'i'), for example:
I 1010
K _EXE
F 1234
R Whatever
R More
R BlaBla

<c>
<p>_EXE</p>
<t>
<ifflag param="1010">
<then>
<setflag param="1234"/>
<r>
<li>Whatever</li>
<li>More</li>
<li>BlaBla</li>
</then>
</ifflag>
</r>
</t>
</c>

The two problems are: 1) Getting the </then>\n</ifflag> properly added at the end of such blocks,
2) Getting the '%' ignored *only* in such blocks (instead of <p>%_EXE</p>, do <p>_EXE</p>)

Anyways, here is the program's code:

#!/usr/bin/perl use strict; #use warnings; my $pipe = ''; my $pipe_content= ''; my $pipe_close = ''; my $tag = { K =&gt; { open =&gt; '%', close =&gt; '', block =&gt; 1, block_open =&gt; "&lt;c&gt;\n".'&lt;p&gt;', block_close =&gt; '&lt;/p&gt;'."\n&lt;t&gt;" }, R =&gt; { open =&gt; '&lt;li&gt;', close =&gt; '&lt;/li&gt;'."\n", block =&gt; 1, block_open =&gt; '&lt;r&gt;'."\n", block_close =&gt; '&lt;/r&gt;'."\n&lt;/t&gt;\n&lt; +/c&gt;" }, S =&gt; { open =&gt; '&lt;set name="temant"&gt;', close =&gt; '&lt;/set&gt;', block =&gt; 0 }, F =&gt; { open =&gt; '&lt;setflag param="', close =&gt; '"/&gt;', block =&gt; 0, }, '#' =&gt; { open =&gt; '&lt;!-- ', close =&gt; ' --&gt;', block =&gt; 0 } }; my $block_flag = 0; my $line_out = ''; my $output = ''; open(DATA,"file"); while (my $line_in = &lt;DATA&gt;) { chomp($line_in); next if $line_in =~ /^C/; next if $line_in =~ /^M/; $line_in =~ s/?/\&aacute\;/g; #standard substitute &aacute; to + "&amp;aacute;", just ignore these lines $line_in =~ s/?/\&Aacute\;/g; $line_in =~ s/?/\&eacute\;/g; $line_in =~ s/?/\&Eacute\;/g; $line_in =~ s/?/\&iacute\;/g; $line_in =~ s/?/\&Iacute\;/g; $line_in =~ s/?/\&oacute\;/g; $line_in =~ s/?/\&Oacute\;/g; $line_in =~ s/?/\&uacute\;/g; $line_in =~ s/?/\&Uacute\;/g; $line_in =~ s/?/\&ntilde\;/g; $line_in =~ s/?/\&iquest\;/g; my ( $style, $content ) = $line_in =~ /^(\w|\#)\s+(.*)$/; # next if $style eq '#' and $content eq ''; do not enable, brings up +even more problems if ($style eq "I") { $pipe = 'yes'; $pipe_content = $content; } next if $line_in =~ /^I/; if ($style eq 'F' and $pipe eq 'yes') { $output = join( '', $output, $tag-&gt;{ $block_flag }-&gt;{ block_close }, "\n".'&lt;ifflag param="'.$pip +e_content.'"&gt;'."\n", '&lt;then&gt;'."\n", '&lt;setflag param="'.$content +.'"/&gt;'."\n"); $pipe = ''; $pipe_content = ''; $pipe_close = 'yes'; #controls when should the &lt;/then&gt;&l +t;/ifflag&gt; should be added (this is what isn't working properly) } elsif ($style eq 'K' and $pipe eq 'yes') { $output = join( '', '&lt;c&gt;'."\n", '&lt;p&gt;'.$content, $output, $tag-&gt;{ $block_flag }-&gt;{ + block_close }, "&lt;/p&gt;\n".'&lt;ifflag par +am="'.$pipe_content.'"&gt;'."\n", '&lt;then&gt;'."\n"); $pipe = ''; $pipe_content = ''; $pipe_close = 'yes'; #controls when should the &lt;/then&gt;&l +t;/ifflag&gt; should be added (this is what isn't working properly) } else { if ($pipe_close eq 'yes') { #this whole procedure is to basically change the block_close for R... +i hope there is a better and easier way my $tag = { K =&gt; { open =&gt; '%', close =&gt; '', block =&gt; 1, block_open =&gt; "&lt;c&gt;\n".'&lt;p&gt;', block_close =&gt; '&lt;/p&gt;'."\n&lt;t&gt;" }, R =&gt; { open =&gt; '&lt;li&gt;', close =&gt; '&lt;/li&gt;'."\n", block =&gt; 1, block_open =&gt; '&lt;r&gt;'."\n", block_close =&gt; '&lt;/then&gt;'."\n".'&lt;/iffla +g&gt;'."\n".'&lt;/r&gt;'."\n&lt;/t&gt;\n&lt;/c&gt;" }, S =&gt; { open =&gt; '&lt;set name="temant"&gt;', close =&gt; '&lt;/set&gt;', block =&gt; 0 }, F =&gt; { open =&gt; '&lt;setflag param="', close =&gt; '"/&gt;', block =&gt; 0, }, '#' =&gt; { open =&gt; '&lt;!-- ', close =&gt; ' --&gt;', block =&gt; 0 } }; $pipe_close = ''; } $line_out = join( '', $tag-&gt;{ $style }-&gt;{ open }, $content, $tag-&gt;{ $style }-&gt;{ close } ); if ( $tag-&gt;{ $style }-&gt;{ block } and ! $block_flag ){ $output = join( '', $output, $tag-&gt;{ $style }-&gt;{ block_open }, $line_out ); $block_flag = $style; } elsif ( ! $tag-&gt;{ $style }-&gt;{ block } and $block_flag){ $output = join( '', $output, $tag-&gt;{ $block_flag }-&gt;{ block_close }, "\n", $line_out, "\n" ); $block_flag = ''; } elsif ( ! $tag-&gt;{ $style }-&gt;{ block }){ $output = join( '', $output, $line_out, "\n" ); } else{ if ($style eq "K") { #this is another exception, don't worry a +bout it $output = join( '', $output, ",", $line_out ); } else { $output = join( '', $output, $line_out ); } $line_out = ''; } } } open OUT, "&gt;", "output.txt" or die; print OUT $output; print OUT "&lt;r&gt;\n&lt;t&gt;\n&lt;c&gt;\n"; #this is because the ve +ry last block's ending tags aren't being processed, no biggie close(OUT);

Hope anyone can help, and truly thank you for taking the time to read all this.


Best Regards,
Marcos

janitored by ybiC: Balanced <code> tags around codeblock instead of HTML markup formatting, and balanced <readmore>'s as well, retitle from less-than-descriptive "Further help.." for better search results


In reply to Help translating into HTML by mrxg4

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.