Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number


by mrxg4 (Initiate)
on Jul 10, 2004 at 03:06 UTC ( #373301=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks,
I have been briefly been working with Perl and have mainly been using it to automate my daily tasks. Now, I'm writting a script that reads from a file, and analyses the first character of every line to format it.

For instance, if there is a line that reads "R Whatever", it becomes <li>Whatever</li>. That's all easy and has been written already.

The problem actually lies with another type of format. Example:
K Perl
K Monks
K Is
K Cool

Here, I need it to be outputted as:

Since I'm reading every line individually I'm having a hard time formatting it this way, and especially knowing when to put the ending </p> tag.

Any help is greatly appreciated. Thanks for taking your time to read this :)


Replies are listed 'Best First'.
Re: Formatting
by atcroft (Abbot) on Jul 10, 2004 at 03:28 UTC

    Just off the top of my head, could you not do something like this (untested code):

    #!/usr/bin/perl -w use strict; my $infilename = 'blah.dat'; my $outfilename = 'blah.out'; my (%data); open(DF, $infilename) or die("Can't open $infilename for input: $!\n"); while (my $line = <DF>) { chomp($line); my @parts = split(/\s/, $line, 2); push(@{$data{$parts[0]}}, $parts[1]); } close(DF); open(OUTF, '>' . $outfilename) or die("Can't open $outfile for output: $!\n"); foreach my $k (sort(keys(%data))) { print OUTF "<p>", join(', ', @{$data{$k}}), "</p>\n"; } close(OUTF);

    Basically, the idea would be to push the data into an array, then join them as you liked.

    Hope that helps....

Re: Formatting
by wfsp (Abbot) on Jul 10, 2004 at 11:45 UTC
    Sometimes it's best to create a table and lookup the formatting as you iterate through the file.
      First of all, thanks to all who replied!

      John, I like you sample script because, like you said, its flexible. I've tried it out and it works all well except one thing, after a </li>, when it has to start with another <li>, it does it all in the same line. I've tried adding a \n to the close string, but it isn't working, its getting outputted instead of interpreted.

      Like I said before, I'm new in Perl and this is probably really easy to fix.

      Thanks again! I really appreciate it :)
        Glad you liked it. I've put another condition in the if block:
        if ( $tag->{ $style }->{ block } and ! $block_flag ){ $output = join( '', $output, $tag->{ $style }->{ block_open }, $line_out ); $block_flag = $style; } elsif ( ! $tag->{ $style }->{ block } and $block_flag ){ $output = join( '', $output, $tag->{ $block_flag }->{ block_close }, "\n", $line_out, "\n" ); $block_flag = ''; } elsif ( ! $tag->{ $style }->{ block } ){ $output = join( '', $output, $line_out, "\n" ); } else{ $output = join( '', $output, $line_out ); }
Re: Formatting
by murugu (Curate) on Jul 10, 2004 at 04:35 UTC

    Iam not a great guy in regular expression.

    I have tried a little with regular expressions.

    use strict; use warnings; undef $/; my $str; ($str=<DATA>)=~s{(((\n|^)K (.*?)(?=\n|$))+)}{"$3<p>".${($_=$1)=~s!(\n| +^)K !,%!g,\$_}."</p>"}eg; $str=~s/(<p>),/$1/g; print $str;

    Please forgive any thing i have done silly in the above code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://373301]
Approved by Paladin
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2020-10-24 01:45 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.