Hi perl Monks, I am a novice perl proprammer. I have developed perl code for html output (two levels list) from short-coded text file. I think the codings are too large. Is there any short way to accomplish it

My input:

<H1>some text</H1> <NLI>some text</NLI> <NLI>some text</NLI> <1NLI>some text</1NLI> <1NLI>some text</1NLI> <2NLI>some text</2NLI> <2NLI>some text</2NLI> <NLI>some text</NLI> <H2>some text</H2> <ALI>some text</ALI> <ALI>some text</ALI> <1NLI>some text</1NLI> <1NLI>some text</1NLI> <2NLI>some text</2NLI> <2NLI>some text</2NLI> <NLI>some text</NLI> <H2>some text</H2> <RLI>some text</RLI> <rLI>some text</rLI> <1NLI>some text</1NLI> <1NLI>some text</1NLI> <2NLI>some text</2NLI> <2NLI>some text</2NLI> <aLI>some text</aLI>

My output:

<H1>some text</H1> <OL TYPE="1"> <LI>some text</LI> <LI>some text <OL TYPE"1"> <LI>some text</LI> <LI>some text <OL TYPE"1"> <LI>some text</LI> <LI>some text</LI> </OL></LI> </OL></LI> <LI>some text</LI> </OL> <H2>some text</H2> <OL TYPE="A"> <LI>some text</LI> <LI>some text <OL TYPE"A"> <LI>some text</LI> <LI>some text <OL TYPE"1"> <LI>some text</LI> <LI>some text</LI> </OL></LI> </OL></LI> </OL> <OL TYPE="1"> <LI>some text</LI> </OL> <H2>some text</H2> <OL TYPE="I"> <LI>some text</LI> </OL> <OL TYPE="i"> <LI>some text <OL TYPE"i"> <LI>some text</LI> <LI>some text <OL TYPE"1"> <LI>some text</LI> <LI>some text</LI> </OL></LI> </OL></LI> </OL> <OL TYPE="a"> <LI>some text</LI> </OL>

My codings:

undef $/; %oli = ("NLI" => "OL TYPE\"1\"", "ALI" => "OL TYPE\"A\"", "aLI" => "OL + TYPE\"a\"", "RLI" => "OL TYPE\"I\"", "rLI" => "OL TYPE\"i\""); open (FIN, "< C:\in.txt") || die "can't open"; $content = <FIN>; $content =~s /(<NLI>.*?\n)(?!<(?:[0-9]?NLI|[0-9](?:B|A|a|R|r)LI)>)/<OL + TYPE="1">\n$1<\/OL>\n/gs; $content =~s /(<BLI>.*?\n)(?!<(?:[0-9]?BLI|[0-9](?:N|A|a|R|r)LI)>)/<UL + TYPE="DISC">\n$1<\/UL>\n/gs; $content =~s /(<ALI>.*?\n)(?!<(?:[0-9]?ALI|[0-9](?:B|N|a|R|r)LI)>)/<OL + TYPE="A">\n$1<\/OL>\n/gs; $content =~s /(<aLI>.*?\n)(?!<(?:[0-9]?aLI|[0-9](?:B|N|A|R|r)LI)>)/<OL + TYPE="a">\n$1<\/OL>\n/gs; $content =~s /(<RLI>.*?\n)(?!<(?:[0-9]?RLI|[0-9](?:B|N|A|a|r)LI)>)/<OL + TYPE="I">\n$1<\/OL>\n/gs; $content =~s /(<rLI>.*?\n)(?!<(?:[0-9]?rLI|[0-9](?:B|N|A|a|R)LI)>)/<OL + TYPE="i">\n$1<\/OL>\n/gs; $content =~s /(<(OL|UL)[^>]*>)((?:(?!<\/\2>)|.)*?)(<\/\2>)/$1.&rep($3. +$4)/gse; sub rep{ my ($line) = @_; $line =~s /<(\/?)(?:N|B|A|a|R|r)LI>(?!\n<[0-9])/<$1LI>/gs; $line =~s /<\/((?:N|A|a|R|r)LI)>(.*?\n)(?=(?:<LI>|<\/OL>))/\n<$oli{$1} +>$2<\/OL><\/LI>\n/gs; $line =~s /<\/BLI>(.*?\n)(?=(?:<LI>|<\/OL>))/\n<UL TYPE="DISC">$1<\/OL +><\/LI>\n/gs; $line =~s /(<(OL|UL)[^>]*>)((?:(?!<\/\2>)|.)*?)(<\/\2>)/$1.&rep1($3.$4 +)/gse; return "$line"; } sub rep1{ my ($line) = @_; $line =~s /<(\/?)1(?:N|B|A|a|R|r)LI>(?!\n<2)/<$1LI>/gs; $line =~s /<\/1((?:N|A|a|R|r)LI)>(.*?\n)(?=(?:<LI>|<\/OL>))/\n<$oli{$1 +}>$2<\/OL><\/LI>\n/gs; $line =~s /<\/1BLI>(.*?\n)(?=(?:<LI>|<\/OL>))/\n<UL TYPE="DISC">$1<\/O +L><\/LI>\n/gs; $line =~s /<(\/?)2(?:N|B|A|a|R|r)LI>/<$1LI>/gs; return "$line"; } open (FOUT, "> C:\out.txt") || die "can't open"; print FOUT $content;

Thanks

Aakikce


In reply to Nested list - to reduce Codings by aakikce

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.