in reply to How to add hyperlink for every table data using perl?

Hyperliked to what?

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

Replies are listed 'Best First'.
Re^2: How to add hyperlink for every table data using perl?
by finddata (Sexton) on Mar 09, 2017 at 09:53 UTC
    Here you had not initialized any hash then then how it possible to pass $href_to_what

      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}

        that href will be the folder and sub folder names (i.e from $file).Here the file includes the folder path.
Re^2: How to add hyperlink for every table data using perl?
by finddata (Sexton) on Mar 10, 2017 at 04:33 UTC
    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"; } }
Re^2: How to add hyperlink for every table data using perl?
by finddata (Sexton) on Mar 10, 2017 at 04:34 UTC

    I had posted my entire part of code .Help me to fix issue where i struck with.Thanks in advance

      I dont understand this Help me to fix issue where i struck with.

      but im going to guess its at

      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.

        Hi, the link is as follows: http://cad.psd.log.com/apps/dev/adkb/dc_checklist/DEMO/top/DV/
        Name Last modified Size Description [DIR] Parent Directory - [DIR] DV/ 07-Mar-2017 12:24 - [ ] block.config 07-Mar-2017 12:24 399 [ ] block.html 07-Mar-2017 12:24 1.6K
        http://cad.psd.log.com/apps/dev/adkb(this is the constant location in which directories are stored).
        i want to link to the the file with its folder location.
Re^2: How to add hyperlink for every table data using perl?
by finddata (Sexton) on Mar 09, 2017 at 06:35 UTC
    <a href="' .$href_to_what{$word} .'">'
    can you explain this part

    Content restored and code tags added by GrandFather

      $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

        what is the purpose of (.) you added in front of every line ?
      How can i hyperlink only the first column of the table using perl.The above code will hyperlink all contents of table data>

        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">]; my $ct=0; for my $word(@data){ $ct++; print $fh_out '<td>'; if ($ct==1){ print $fh_out '<a href="' .$href_to_what{$word} .'">'; } print $fh_out $word; if ($ct==1){ print $fh_out '</a>'; } print $fh_out '</td>'; } print $fh_out "</tr>\n"; }
        Untested, YMMV