Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Creating table through PERL

by sid.verycool (Novice)
on Apr 23, 2013 at 09:26 UTC ( #1030084=perlquestion: print w/ replies, xml ) Need Help??
sid.verycool has asked for the wisdom of the Perl Monks concerning the following question:

Hi all I need to create a table and the values inside the table are to be fetched by running a shell script

The steps involved are as follows Example:
#> ./script1 OUTPUT: A 3 B 5 C 1 and so on #> ./script1 Output: A 6 B 2 X 8

Basically im getting some values for each user (A,B,C etc) These are the results in various subjects i.e script1 gives me marks of students in Maths, script2 gives marks of students in Science etc. Note: script1, 2 are shell scripts returning output on screen.

Now I need this data as a table or categorised form html page. i.e. A page having headings like MATHS SCIENCE SST in the rows I have users (A,B,C) and each column contains marks for the user taken from script1, script2 upto script n for n subjects

So for doing this should I have a template html befor or i can have a script that makes such HTML page on the go. eg I run the master script -< which in turn runs script1,2...n and create the corresponding html. I have changed my problem to make it more easy, this now looks a bit easier and more doable. I'm new to forum and dont know if I should have placed this as a new question. Many thanks

Comment on Creating table through PERL
Download Code
Re: Creating table through PERL
by mv.ashwin@gmail.com (Novice) on Apr 23, 2013 at 10:38 UTC

    you can create a table as below.

    print $FileHandler "column1 column2 column3\n"; print $FileHandler "-------------------------------------------------- +------\n"; printf($FileHandler "%-27s%-25s%s\n", $column1_data, $column2_data, $c +olumn3_data);

    For html try the below

    print HTML "<table border = '1'> <tr><td><center>Column1</center> </td +><td style='word-wrap:break-word'><center>Column2</center></td><td st +yle='word-wrap:break-word'><center>Column3</center></td></tr>"; print HTML "<tr><td style='word-wrap:break-word'> $column1_data</td><t +d style='word-wrap:break-word'>$column2_data</td><td style='word-wrap +:break-word'>$column3_data</td></tr>";
Re: Creating table through PERL
by clueless newbie (Friar) on Apr 23, 2013 at 11:36 UTC
Re: Creating table through PERL
by 2teez (Priest) on Apr 23, 2013 at 15:46 UTC

    Hi sid.vertcool,
    If I may add a few point here.

    ~ I need to create a table..how do i start my work
    There are several ways you can do this. You can use Text::Table as mentioned by clueless newbie, one can also use Perl6::Form, infact, you can use printf.
    See below for some example, using both Perl6::Form and Text::Table.

    #!/usr/bin/perl use warnings; use strict; use Perl6::Form; my $student_data; chomp( my $title = <DATA> ); push @{$student_data}, [split] while <DATA>; print "\n\nFIRST TABLE:\n"; print form "==============================================", "| STD_NAME | STD_NUM | SUB_A | SUB_B | SUB_C |", "=============================================="; print form "| {<<<<<<} |{||||||} | {||||}|{|||||}|{|||||}|", $_->[0], $_->[1], $_->[2], $_->[3], $_->[4] for @$student_data; use Text::Table; print "\n\nSECOND TABLE:\n"; my $plain_table = Text::Table->new( split /\s+/, $title ); $plain_table->load( [@$_] ) for @$student_data; print $plain_table; __DATA__ student_name student_number subject_A subject_B subject_C zadok 3006 45 67 -- mechi 2917 67 89 45 judas 3010 20 -- 12 temin 1122 60 56 90
    Output: First Table is by Perl6::Form and the Second Table by Text::Table
    FIRST TABLE: ============================================== | STD_NAME | STD_NUM | SUB_A | SUB_B | SUB_C | ============================================== | zadok | 3006 | 45 | 67 | -- | | mechi | 2917 | 67 | 89 | 45 | | judas | 3010 | 20 | -- | 12 | | temin | 1122 | 60 | 56 | 90 | SECOND TABLE: student_name student_number subject_A subject_B subject_C zadok 3006 45 67 -- mechi 2917 67 89 45 judas 3010 20 -- 12 temin 1122 60 56 90
    NOTE: Please, note that I don't know how the OP is getting his/her data as shown in the Original Post, I only used what I can come up with for data here.
    Infact, you can even write your data to an Excel file directly if you wish using module like Spreadsheet::WriteExcel and the likes.
    That been said, it's important to say, there is nothing like PERL as Perl is not an acronym see What's the difference between "perl" and "Perl"?
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Practical Extraction and Reporting Language http://en.wikipedia.org/wiki/Perl and thanks for correcting me 2teez, I missed that this is "NON OFFICIAL"

        Please, also check this History->Names and note this line "..There is some contention about the all-caps spelling "PERL", which the documentation declares incorrect and which some core community members consider a sign of outsiders.."
        You are welcome sid.verycool.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Creating table through PERL
by hpham01 (Initiate) on Apr 24, 2013 at 00:09 UTC

    Since you want to generate a table in HTML, perhaps you could use the Module HTML::Table which you can download from CPAN. There is an example of how to use the module on CPAN

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030084]
Approved by Corion
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-10-23 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls