Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:04:20+00:00 2026-06-04T17:04:20+00:00

I try to create a HTML table by hardcoding HTML mark-up in a Perl

  • 0

I try to create a HTML table by hardcoding HTML mark-up in a Perl script. I get data from a text file and then split them up by component and print them out into a HTML table. Example of text file (author, book name, book content):

Ronnie Smith, javabook, javaclasses
Ronnie Smith, javabook, javamethods
Ronnie Smith,  c-book, pointers
Carrlos Bater, htmlbook, htmltables

How do I just print the author name once instead of printing three times, and the same with the book name? Only one author, Ronnie Smith, wrote three books, so it should fall under one category. Also e.g. javaclasses and javamethods are from same book javabook, so we should have javabook printed only once.

My script:

use strict;
use warnings;

my $Author;
my $bookName;
my $bookContent;
my $bookContentList = "List.txt";

open MYFILE, $bookContentList or die "could not open $bookContentList \n";

my @body = "";
push(@body, "<html> \n");
push(@body, "<head> \n");
push(@body, "<TABLE BORDER=\"0\"\n");
push(@body, " <TD>");
push(@body, "<div align=\"left\"><Table border=1 bordercolor= \"black\"> \n");
push(@body, "<tr bgcolor=\"white\"><TH><b>Author</b></TH><TH>Book Name     </TH><TH>bookContent</TH></TR>");
push(@body, "<br>\n");

while (<MYFILE>) {
    ($Author, $bookName, $bookContent) = split(",");
    push(
        @body, "<TR><td>$Author</TD>
         <td>$bookName</TD>
         <td>$bookContent</TD>"
    );
}
push(@body, "</div></Table>");

my $Joining = join('', @body);
push(@body, "</body></font>");
push(@body, "</html>");
my $htmlfile = "compliance.html";
open(HTML, ">$htmlfile") || warn("Can not create file");
print HTML "$Joining";
close HTML;
close MYFILE;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:04:22+00:00Added an answer on June 4, 2026 at 5:04 pm

    This is a classic case for a hashtable: You want to identify an author and a book by unique identifiers:

    my ( %author_books, @author_order );
    while (<MYFILE>) {
        ($author, $bookName, $bookContent) = split(",");
        my $auth = $author_books{ $author };
        unless ( $auth ) { 
            push @author_order, $author;
            $author_books{ $author } = $auth = {};
        }
        my $books= $auth->{books}{ $bookName };
        unless ( $books) { 
            push @{ $auth->{order} }, $bookName;
            $auth->{books}{ $bookName } = $books= [];
        }
        push @$books, $bookContent;
    }
    
    foreach my $author ( @author_order ) { 
        my $auth  = $author_books{ $author };
        my $books = $auth->{order}; 
        push @body, qq{<tr><td rowspan="${\scalar @$books}">$author</td>\n};
        my $line = 0;
        foreach my $book ( @$books ) { 
            push @body, '<tr><td></td>' if ++$line;
            push @body
               , ( "<td>$book</td><td>"
                 . join( "<br/>\n", @{ $auth->{books}{ $book } } )
                 . "</td><tr>\n"
                 );
        }
    }
    

    I do not recommend the push @body method. But if you’re going to do it, I would recommend File::Slurp.

    File::Slurp::write_file( $htmlfile, @body );
    

    That way you also skip the mistake of joining $Joining too soon and failing to write out the footer, like you do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a HTML table with links from a XML file using
I lern Python and try to do parcer for html table and then I
I try to create a binary tree as html-table which is not recursive build.
I try create two moving object from one class. I want to control they
I have specific troubles when I try create node. From context menu everything gona
I want try to create this screen, but text fields on the right side
<%using (Html.BeginForm(OrderDevice, ImportXML, FormMethod.Post)) { %> <table id=OrderDevices class=data-table> <tr> <th> DeviceId </th> <th>
I try to update a column in a temporary table by concatenating rows from
i use this class for create HTML content http://code.google.com/p/php-class-html-generator/ I try to build a
I have a html table built from a DB. It has a Edit button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.