in reply to Re^4: Formatting
in thread Formatting
Does a # in the data indicate a comment? If it does the code below skips lines begining with a #. If I have not understood you correctly, please give an example of the data.
The code also adds a comma to each 'inline' style.
I've added a variable to help make the code clearer.
Health warning
There is more than one way to do this! This is how I do it and it is heavily influenced by the fact that my main experience in coding is vb which makes most people around here shudder! For example, in Programing Perl, a long list of elsifs is frowned on. I tell myself that as I'm using the less well known 'baby idiom' (to be polite) it is ok. Until, at least, I learn a better way.
Also my main motive for helping is to hone my own skills (which badly need honing).
while (my $line_in = <DATA>) { chomp($line_in); next if $line_in =~ /^#/; # skip comments my ( $style, $content ) = $line_in =~ /^(\w)\s+(.*)$/; my $block = $tag->{ $style }->{ block }; $line_out = join( '', $tag->{ $style }->{ open }, $content, $tag->{ $style }->{ close } ); if ( $block and ! $block_flag ){ $output = join( '', $output, $tag->{ $style }->{ block_open }, $line_out, ',' ); $block_flag = $style; } elsif ( ! $block and $block_flag ){ chop $output; # remove final comma $output = join( '', $output, $tag->{ $block_flag }->{ block_close }, "\n", $line_out, "\n" ); $block_flag = ''; } elsif ( $block ){ $output = join( '', $output, $line_out, ',' ); } else{ $output = join( '', $output, $line_out, "\n" ); } $line_out = ''; } # data with comments __DATA__ R Whatever # First comment R Another whatever K Perl K Monks # Second comment K Is K Cool T Another style R Whatever
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Formatting
by mrxg4 (Initiate) on Jul 12, 2004 at 21:05 UTC | |
by wfsp (Abbot) on Jul 13, 2004 at 11:14 UTC | |
by mrxg4 (Initiate) on Jul 13, 2004 at 13:57 UTC | |
by mrxg4 (Initiate) on Jul 14, 2004 at 13:58 UTC |