mrxg4 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Help translating into HTML
by wfsp (Abbot) on Jul 17, 2004 at 09:16 UTC
    Hi,
    I may be prepared to have a go but there is a condition!
    Have you given all the possible format variations? The last time you started off with a fairly straight forward question and I came up with a simple algorithm (the table) as a starting point.
    You then proceeded to drip feed more permutations which I tried to incorporate. As you noticed, I gave up in the end.
    Sometimes, rather than trying to code yourself out of a problem, it is best to start again. This may be one of those occasions. I'd still go the table route but change the structure.
    So, three questions: Have you given all the permutations? Have you given all the permutations? Have you given all the permutations?
    Sorry to be sarcastic but last time was a bit exasperating. Formatting
    Updated: Fixed typo
      Alright, you're right, I haven't mentioned a few, but here we go.

      First of all, lines that begin with C or M are ignored (but in the case of C it specifies the beginning of the block, so in that case everything after the C should be ignored and finally written as <c>)

      Then, lines that begin with S, are written as: <set name="temant"> </set>, after the <t>.

      The # are comments, and should be written as <!-- whatever -->, but this is already done, as you probably noticed.

      One more thing, sometimes, a block entry might have many K's (or 1, doesn't matter), but no S or T (and then R's). Please note that S and T are *rarely* within the same block, if there is no T then theres usually an S instead. I've done all this successfully in the last code I sent you, so you can check that from there.

      Hope this is clear and thanks for all the help you've given so far :)

      Kind Regards,
      Marcos
Re: Help translating into HTML
by wfsp (Abbot) on Jul 17, 2004 at 16:21 UTC
    Hi,
    It would help me (and possible others) if you listed all the rules you have.
    It would save me going back through the posts and getting it wrong.

    Updated: Added explanation.

      Here is everything altogether:

      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>

      Additionally, lines that begin with C or M are ignored (but in the case of C it specifies the beginning of the block, so in that case everything after the C should be ignored and finally written as <c>)

      Then, lines that begin with S, are written as: <set name="temant"> </set>, after the <t>.

      The # are comments, and should be written as <!-- whatever -->, but this is already done, as you probably noticed.

      One more thing, sometimes, a block entry might have many K's (or 1, doesn't matter), but no S or T (and then R's). Please note that S and T are *rarely* within the same block, if there is no T then theres usually an S instead. I've done all this successfully in the last code I sent you, so you can check that from there.

      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>)

      Ok, like I said, thats pretty much it, thanks again.

      Kind regards,
      Marcos
Re: Help translating into HTML
by wfsp (Abbot) on Jul 17, 2004 at 20:40 UTC
    The first example includes:
    K One K Two
    in the input but doesn't appear to be in the output.
Re: Help translating into HTML
by wfsp (Abbot) on Jul 17, 2004 at 20:49 UTC
    Also, for an 'I' block you gave the following example output:
    <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>
    Should the '</r>' occur before '</then>'. It looks a bit unbalanced.
      Regarding your first question, yes that was my mistake, I forgot to include the "One" and "Two" in the output. I did it manually so.. =P

      As for this question, I know it looks unbalanced and I perfectly understand, but thats the way it should be.

      Thanks again.

      Kind Regards,
      Marcos
Re: Help translating into HTML
by wfsp (Abbot) on Jul 18, 2004 at 02:00 UTC
    I abandoned the table. I've hard coded the logic into the loop and moved some of the work out of it.
    #!/bin/perl5 use strict; use warnings; my ( @K_array, @R_array ) = (); my $I = ''; my $out = qq(<c>\n); while (my $l = <DATA>) { my ( $code, $value ) = $l =~ /^(\w|\#)\s+(.*)\n$/; $I = $value if $code eq 'I'; if ( $code eq 'K' ){ $out .= "<p></p>\n<t>\n" unless @K_array; push @K_array, $value } elsif ( $code eq 'R' ){ $out .= "<r></r>\n" unless @R_array; push @R_array, "<li>$value</li>\n" } $out .= qq(<set name="temant">$value</set>\n) if $code eq 'S'; $out .= qq(<setflag param="$value"/>\n) if $code eq 'F'; $out .= "<!-- $value -->\n" if $code eq '#' } my $K; if ( $I ){ $K = $K_array[0] # assumes only 1 K with I } else{ $K .= '%' . $_ . ',' for @K_array; chop $K } $out =~ s|<p></p>|<p>$K</p>|; my $R = join '', @R_array; $out =~ s|<r></r>|<r>\n$R</r>|; $out .= qq(</t>\n</c>); if ( $I ){ $out =~ s|<t>|<t>\n<ifflag param="$I">\n<then>|; $out =~ s|</r>|</then>\n</ifflag>\n</r>|; } print "$out\n"; __DATA__ C 1010 K Perl K is K cool F 1234 S Alright R Whatever R More R BlaBla
      While this appears to have potential, right now it only prints out lines that begin with an R, S or F, and completely ignoring the block structure itself.

      By the way, some blocks don't even have an S or T, so, that shouldn't be depended on.

      I also like the idea of hard-coding it all into the application, it avoids a whole lot of problems.

      Thanks.

      Kind Regards,
      Marcos
Re: Help translating into HTML
by wfsp (Abbot) on Jul 17, 2004 at 18:30 UTC
    Starting to get a bit clearer now!
    Could you give an example of input and output for blocks that have lines begining with M, S, T and #.
    You mention them so I want to be clear.

    Loading the input into a hash may be the way to go.

      Ok,
      Lines with M are *completely* ignored.

      Lines that begin with a # can be found pretty much anywhere, be it at the beginning, middle or end of a block. But they should be enclosed in <!-- -->. Think of these as individual lines, that don't have any effect in the block structure itself.

      There are no T's, I confused them with the F's. Sorry about that.
      Now, as for the S and F's, here are some examples:
      1) Input:
      K One
      K Two
      S Alright
      R Whatever?
      R Blablabla
      R And even more
      1) Output:
      <c>
      <t>
      <set name="temant>Alright</set>
      <r>
      <li>Whatever?</li>
      <li>Blablabla</li>
      <li>And even more</li>
      </r>
      </t>
      </c>

      2) Input:
      K Perl
      K Monks
      S Theme
      F 1234
      R Examples
      R More
      R Yes
      2) Output:
      <c>
      <p>%Perl,%Monks</p>
      <t>
      <set name="temant">Theme</set>
      <setflag param="1234"/>
      <r>
      <li>Examples</li>
      <li>More</li>
      <li>Yes</li>
      </r>
      </t>
      </c>

      On a seperate note, there may be special blocks that don't have either an S or an F. Example:
      K Special
      K Case
      R One
      R Two
      R Three
      Here, it translates to:
      <c>
      <p>%Special,%Case</p>
      <t>
      <r>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
      </r>
      </t>
      </c>

      Hopefully this should clear things up. If you have any other questions, don't hesitate to ask, thanks.

      Kind Regards,
      Marcos
Re: Help translating into HTML
by wfsp (Abbot) on Jul 18, 2004 at 05:31 UTC
    I get these results:

    Input with C:

    C 1010 K Perl K is K cool F 1234 S Alright R Whatever R More R BlaBla
    Output with C:
    <c> <p>%Perl,%is,%cool</p> <t> <setflag param="1234"/> <set name="temant">Alright</set> <r> <li>Whatever</li> <li>More</li> <li>BlaBla</li> </r> </t> </c>
    Input with I:
    I 1010 K _EXE F 1234 R Whatever R More R BlaBla
    Output with I:
    <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>
    Input special case:
    K Special K Case R One R Two R Three
    Output special case:
    <c> <p>%Special,%Case</p> <t> <r> <li>One</li> <li>Two</li> <li>Three </li> </r> </t> </c>
    Is this what you get?
      Yeah, when you set the DATA as individual blocks such as the ones you demo'ed, then it works flawlessly, but try setting DATA to say..

      C 90
      K One
      K Two
      F 1234
      R This
      R Is
      R The
      R First
      R Example
      #
      K Second
      F 4321
      R Second
      R Example
      #
      # Some
      # Comments
      #
      K Last
      F 0101
      F 0202
      F 0303
      R This
      R Is
      R The
      R Third
      R And
      R Last
      R Example

      Then you'll realize that the problem is the program interprets the whole input as one block, when in reality theres dozens and dozens of them.

      Hope its a bit clearer now.

      Thanks.

      Kind Regards,
      Marcos
        Please John, don't give up now, I know you're close.

        Whats the point of giving up right now..?

        I really appreciate all the help you've given so far, honestly.

        Best Regards,
        Marcos