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 | |
|
Re: Nested list - to reduce Codings
by ptum (Priest) on Sep 26, 2006 at 13:57 UTC | |
by aakikce (Acolyte) on Sep 26, 2006 at 14:05 UTC | |
by ptum (Priest) on Sep 26, 2006 at 14:16 UTC | |
|
Re: Nested list - to reduce Codings
by InfiniteLoop (Hermit) on Sep 26, 2006 at 15:19 UTC | |
by revdiablo (Prior) on Sep 26, 2006 at 17:31 UTC |