Re: How to add hyperlink for every table data using perl?
by kcott (Archbishop) on Mar 09, 2017 at 07:24 UTC
|
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>
| [reply] [d/l] [select] |
|
|
use strict;
use warnings;
use Spreadsheet::HTML qw(generate);
my @data = map { chomp; s/(?:COMMENT|[&\\_@])//g; [split /:/] } <DATA
+>;
my @class = map { map $_ ? 'norm' : 'bold', splice @$_, 0, 1 } @data;
my @frag = map { splice @$_, 2, 1 } @data;
print generate(
data => \@data,
matrix => 1,
indent => ' ',
tr => { class => \@class },
-c2 => sub { sprintf '<a href="#%s">%s</a>', shift(@frag), shift }
);
__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:
<table>
<tr class="bold">
<td>A</td>
<td>B</td>
<td><a href="#frag1">name1</a></td>
</tr>
<tr class="norm">
<td>C</td>
<td>D</td>
<td><a href="#frag2">name2</a></td>
</tr>
<tr class="bold">
<td>E</td>
<td>F</td>
<td><a href="#frag3">name3</a></td>
</tr>
<tr class="norm">
<td>G</td>
<td>H</td>
<td><a href="#frag4">name4</a></td>
</tr>
<tr class="bold">
<td>I</td>
<td>J</td>
<td><a href="#frag5">name5</a></td>
</tr>
</table>
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Have you updated your question without showing the change? Please read How do I change/delete my post?.
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] |
Re: How to add hyperlink for every table data using perl?
by huck (Prior) on Mar 09, 2017 at 05:47 UTC
|
while (my $line=<$fh>) {
chomp $line;
for($line)
{
s/\&//g;
s/[\\\_\@\_]//g;
s/COMMENT//g;
}
my @data = split /:/, $line;
my $class = $data[0] ? 'normal' : 'bold';
print $fh_out qq[<tr class="$class">];
for my $word(@data){
print $fh_out '<td><a href="'
.$href_to_what{$word}
.'">'
.$word
.'</a></td>';
}
print $fh_out "</tr>\n";
}
Untested, YMMV
| [reply] [d/l] |
|
|
Here you had not initialized any hash then then how it possible to pass $href_to_what
| [reply] |
|
|
You have yet to say what the hrefs should be and where they will come from, how can i guess what should be in %href_to_what? Remember my first question was "Hyperliked to what?", thats why it is named as it is. I expected you to know what you wanted in there.
For each word in $data[0] you need to somehow predetermine what is in $href_to_what{$word}
| [reply] [d/l] [select] |
|
|
|
|
foreach my $file (@files)
{
my ($dir, $root, $ext) = $file =~ m|(.*)/(.*)\.(.*)|;
#print $dir;
my $outfile = "$dir/$root.html";
#print "Found: $dir/$root", '.', $ext, ". Write: $outfile","\n";
+
open my $fh_out, '>', $outfile or die "Can't open $outfile: $!","\
+n";
my $head = "
<!doctype html>
<html lang=\"en\">
<head> <meta charset=\"utf-8\"> <title>DCMS_CHECKLIST</title><tr><td><
+/td></tr> </head>
<body>
<table>
<th>SL.NO</th><th>CHECKLIST ITEM</th><th>VALUE</th><th>COMMENTS</th><t
+h>CONFIRMATION</th>
<style>
.bold {
font-weight: bold;
}
.bold td
{
border: 0px;
}
table, th, td {
border: 1px solid black;
}
</style>";
print $fh_out $head ; # write the header
open my $fh, '<', $file or die "Can't open $file: $!";
while (my $line=<$fh>) {
chomp $line;
for($line)
{
s/(?:COMMENT|[&\\_@])//g;
}
my @data = split /:/, $line;
my $class = $data[0] ? 'normal' : 'bold';
#print $fh_out qq[<tr class="$class"><td>] . join('</td><td>', spli
+t(/:/,$line)) . "</td></tr>\n";
print $fh_out qq[<tr class="$class">];
# my %data;
my $check=0;
for my $word(@data){
$check++;
print $fh_out '<td>';
if($check==1)
{
print $fh_out '<a href="'.$outfile{$word}.'">';
# print $fh_out '<a href="'.$word.' " >';
}
print $fh_out $word;
if($check==1)
{
print $fh_out '</a>';
}
print $fh_out '</td>';
}
print $fh_out "</tr>\n";
}
}
| [reply] [d/l] |
|
|
| [reply] |
|
|
print $fh_out '<a href="'.$outfile{$word}.'">';
that you are having problems. $outfile is a scalar my $outfile = "$dir/$root.html"; and not an hash so you can t"address into" it with {$word}. Now in $outfile also contains a filesystem name and not a http url. It is quite possible to change $outfile into a uri by knowing your web-server-root directory and changing that subsection at the beginning of $outfile to "http://some.server.name/", or just removing it and creating a relative (to the webserver) uri. But $outfile in itself is not a valid uri.
But even then $outfile is the file you are writing to now, so i find it kinda hard to believe you want a hyperlink to the same webpage you are on now. Maybe you want to hyperlink to another section with a "#subpart" reference, or maybe you want to link to some other page than the one you are on now. But you still have not said where it is you want to link to.
So, if you are printing what is contained in $word into the first column, just where is it that you want to link to. is it based on what is in $word (SL.NO)? Is it based on what goes into the columns CHECKLIST ITEM, or VALUE, or COMMENTS, or CONFIRMATION? if you can tell me what is the uri you want to link the row to, i can explain what to change $outfile{$word} to. If you can construct a variable containing a valid url, like my $href="http:/some.server.name/$part1/$part2/$part3"; replaceing $part1, $part2, $part3 with some combination of $dir, $root, $ext, $data[0], $data[1], $data[2], $data[3],$data[4] just code it before the my $check=0; line and change print $fh_out '<a href="'.$outfile{$word}.'">'; to print $fh_out '<a href="'.$href.'">';
and if that is all of your code, you are still have a lost </table>. i do see the <table> table and <th>...</th> header tags, but you should close your table too. It is nice to close the body and html tags as well, but less required. An unclosed table tag can present an error condition at times.
But i am not a mindreader and i still dont know where you want to link to because you have not told me yet. | [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
|
<a href="'
.$href_to_what{$word}
.'">'
can you explain this part
Content restored and code tags added by GrandFather | [reply] [d/l] |
|
|
$href_to_what{$word} is a hash, indexed by every word where each value for a word contains the link for that word
You need to edit that post and fix it so it displays right, i think you have square braces you dont want to be in there
| [reply] [d/l] |
|
|
|
|
|
|
|
|
How can i hyperlink only the first column of the table using perl.The above code will hyperlink all contents of table data>
| [reply] |
|
|
|
|