This question really has two parts: 1) Parse data into a data structure that can 2) be put into a table that will have a different number of rows depending on the data input.

I can't figure out what to use exactly to organize the data, and the answer may be contingent on how the second part is solved (building of the table). I know a hash won't work because I will have rows of data with "keys" that are duplicated, and that have multiple values. So I maybe I need a multidimensional array? Hopefully my code example below will make it more clear on what I am trying to do. I will be importing data from a file that has data structured as shown in the $file variable (<version number> <environment> <date>). I want to put this data in a table that is ordered by columns that are the environment, and rows that are the version number (the version number in the table corresponds to the module, so version 1.xx = module 1, version 2.xx = module 2, etc.). I am using text::table to build the table, but I realize that might not be the correct choice if I need to dynamically build rows.

Here is a code snippet:

use strict; use warnings; use text::table; use Data::Dumper; sub main { my $file = "1.00 InDev 01-Jun-2013 1.00 InTest 15-Jul-2013 1.00 InUAT 31-Jul-2013 1.00 InProd 15-Sep-2013 1.01 InDev 01-Jul-2013 2.00 InDev 01-Aug-2013 3.00 InDev 01-Sep-2013"; # This seems to be the only way to convert the scalar file to an array # where each line is an array element. There's probably a better way. my @array = split( /\n/, $file ); # This is simply a test to see what the array looks like print Dumper(@array); }; main(); # This next bit of code is not connected to the above because I don't # know the best way to build a data structure for the table. # Therefore I'm just creating a simple dummy array based on what a # single line of the table might look like. In addition, if there # are no matches with an (x,y) location in the table for a certain # row of data, I would like to fill it with the text "N/A", # hard coded as an illustration below. my @tarray = ( "1.0", "06-Mar-2014", "06-Mar-2014", "06-Mar-2014", "06-Mar-2014" +); my $tb = Text::Table->new( "Module", "Version", "InTest", "InUAT", "IN +Prod" ); $tb->load( [ "Module 1", $tarray[0], $tarray[1], $tarray[2], $tarray[3] ], [ "Module 2", "N/A", "N/A", "N/A", "N/A" ], [ "Module 3", "N/A", "N/A", "N/A", "N/A" ], ); print $tb;

Here's an example of the desired output based on the input of $file above:

Module Version InDev InTest InUAT INProd Module 1 1.00 01-Jun-2013 15-Jul-2013 31-Jul-2013 15-Sep-2013 Module 1 1.01 01-Jul-2013 N/A N/A N/A Module 2 2.00 01-Aug-2013 N/A N/A N/A Module 3 3.00 01-Sep-2013 N/A N/A N/A

I realize this is a rather complex, multi-part question, but I thought it would be most clear if entered as one question to understand all the parts. Any and all assistance would be greatly appreciated. Thank you.


In reply to Dynamically build a table by Deep_Plaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.