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

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

Replies are listed 'Best First'.
Re: Nested list - to reduce Codings
by leocharre (Priest) on Sep 26, 2006 at 14:04 UTC

    Hi Aakikce. What is it your program is supposed to do?

    I am guessing what you are doing is developing a mini scripting language that translates pseudo-html to html. I can see the value of that. It's a very nifty project. You could learn a lot doing this.

    If you are trying to create something of actual use (for production, for others to use)- this is a dead pursuit- unless you are some wizard programmer with some master angle.

    Please look at : CGI, creating standard html elements . When I started playing with perl I would do things like code my own super duper "div" sub that would take funny arguments to make a javascript collapse expand block, or style a million ways etc etc. I would create my own "form" sub to output a form a certain way to the screen. It would take a paper to explain why one would use CGI.. so.. Ovid's CGI course lesson two, why use CGI.pm

    A module perhaps best suited to these thigs.. may be HTML::Template.. which at first may seem fruity or needlessly simple ( i had the ignorant impression at first that It was so simple I could just just 'roll my own' in fifteen minutes. (please..)) - has been one of the most powerful tools in my arsenal to separate content from presentation.

    If you take the small pain to learn to use existing tools in perl, that is..the code and modules so lovingly donated by the community to the community.. (aka things written by people who know better then we do but will spend only so much energy pushing us in the right direction)- then you will be much better off and your projects will grow by leaps and bounds, and be based on the solid (hopefuly) work of your forefathers.

    Why CGI, why HTML::Template.. To separate content from presentation. To separate as much as possible .. code from design and content.

    Also look up POD to html on cpan.org - and test to html, also look it up here on perlmonks. You'll find great stuff and amazing solutions that other people found.

    I think the thing you're going to hear a lot from perlmonks is that the question you pose - what you are trying to do, creates a lot of problems and achieves little. It is as if you are asking "i want to get my lamborghini diablo to give me 40mpg, any ideas how i can do that?". Or.. My pet ants refuse to mingle with my praying mantis, how can I therefore keep both pets , mayhbe even make them get along? - Most people would say separate them into different containers, if they are in the same. it is a tried and true solution. but.. Do not be discouraged. If you can figure out how to have both ants and mantid get along in the same container.. you're on to something. Chances are it won't work. But you will learn tons.

    I think it was maybe brian d foy (?) that said "a professional programmer is one who has made all the mistakes"

Re: Nested list - to reduce Codings
by ptum (Priest) on Sep 26, 2006 at 13:57 UTC

    Greetings, aakikce. I find myself a little impatient with this posting, not wanting to read through the whole thing so that I can help you. (Maybe I'm just cranky this morning, though.) You might want to add an Update: to the top of the question, summarizing exactly what you want to do, and perhaps tossing in a few comments that explain what you think your code is doing. Also, re-posting the same question several times is not the best way to get a response from the monastery. You might also want to check out How do I post a question effectively?.

    It looks like you are trying to read in some XML and reformat it into something that looks more like HTML -- you might consider some of the templating modules like HTML::Template to accomplish this.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde

      Hi

      Sorry. To see any replies, just I refresh my browser. It was posted repeatedly. Thanks for your Advice.

        It is not a problem, and by now all the excess nodes have been reaped. It has probably cost you a few experience points, and you've gotten off to a rocky start, but you are welcome here at the monastery. :)

        Since the monks here are offering their expertise for free, you'll need to do your best to make it easy for people to help you. That usually means that you will:

        • Try hard yourself before you post a question
        • Use tutorials, frequently asked questions and Super Search before posting
        • Post your question clearly and effectively
        • Make your question as concise as possible while still showing all the code you need (sometimes this involves creating a very compact contrived example)

        There are lots of other resources for a monk who is just getting started, and you will make the best use of the advice offered here if you spend a few hours reading About the PerlMonks FAQ carefully.

        Welcome! :)

Re: Nested list - to reduce Codings
by InfiniteLoop (Hermit) on Sep 26, 2006 at 15:19 UTC
    Hi,
      CPAN has a number of well documented templating modules, try using them. I reccommend Html::Template.

      Actually, since Perl is case sensitive about package names, that's HTML::Template. It may seem like a small thing, but it can make all the difference between code that works and code that does't.