G'day finddata,
Welcome to the Monastery.
You have quite a few issues with your Perl and HTML code.
In addition, there's important information missing from your post.
-
Your code is poorly laid out.
Please indent to improve readability
and choose whatever style of bracing you want but use it consistently.
See perlstyle for some hints on this.
-
Your for loop is entirely unnecessary.
I suspect you've used it to assign $line to $_
so that you won't need to bind a variable to the substitutions.
Why bother assigning what's read from <$fh> to $line in the first place.
-
You only need one substitution, not three.
You only need to escape one of the characters (the backslash), not five.
You've used \_ twice in the character class.
-
You've performed the same split twice:
replace the second instance with what you got the first time, i.e. @data.
-
print takes a list of arguments.
Additional concatenation operations to provide a scalar argument are unnecessary.
-
In your HTML:
-
the <a> element is not in a <td> element;
-
you've started the fragment identifier but not completed it;
-
the href attribute will need to be quoted; and,
-
you've provided no name for the link.
-
In your post:
-
you've given no indication of your input data;
-
you haven't shown what output you're currently getting; and,
-
you haven't shown what output you actually want.
Please see "How do I post a question effectively?" for more information regarding these posting issues.
Based on your code, I made a very rough guess at what your input might look like.
Here's the guts of what I think you probably want.
You'll no doubt need to make quite a few changes, but this should be a sound starting point.
#!/usr/bin/perl -l
use strict;
use warnings;
while (<DATA>) {
chomp;
s/(?:COMMENT|[&\\_@])//g;
my @data = split /:/;
print '<tr class="', $data[0] ? 'norm' : 'bold', '">',
'<td>', join('</td><td>', @data[1,2]), '</td>',
'<td><a href="#', $data[3], '">', $data[4],
'</a></td></tr>';
}
__DATA__
0:A:B:frag1:name1
1:C:D:frag2:name2
0:E:F:fr&ag3:naCOMMENTme3
1:G:H:fr\ag4:_name4
0:I:J:fr@ag5:name5
Output:
<tr class="bold"><td>A</td><td>B</td><td><a href="#frag1">name1</a></t
+d></tr>
<tr class="norm"><td>C</td><td>D</td><td><a href="#frag2">name2</a></t
+d></tr>
<tr class="bold"><td>E</td><td>F</td><td><a href="#frag3">name3</a></t
+d></tr>
<tr class="norm"><td>G</td><td>H</td><td><a href="#frag4">name4</a></t
+d></tr>
<tr class="bold"><td>I</td><td>J</td><td><a href="#frag5">name5</a></t
+d></tr>
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.