in reply to Dynamically build a table

This may be what you want:

use strict; use warnings; use Data::Dumper; 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"; my @file_content = split( /\n/, $file ); my @full_data; for my $line (@file_content) { my @fields = split /\s+/, $line; push @full_data, \@fields; } print Dumper \@full_data;
which prints the data this way:
$ perl table.pl $VAR1 = [ [ '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' ] ];

The four important lines:

for my $line (@file_content) { my @fields = split /\s+/, $line; push @full_data, \@fields; }
could be made much more concise:
push @full_data, [split] for @file_content;
but I preferred to keep a step by step approach to make understanding easier.

Update:: when I posted the above, Deep_Plaid had not updated the original post with the addition of the desired output. As I said in another post above, it may not be necessary to build this AoA to arrived at the desired output. The solution might be much simpler.

Replies are listed 'Best First'.
Re^2: Dynamically build a table
by Deep_Plaid (Acolyte) on Mar 11, 2014 at 18:33 UTC

    Hi LR. Yes, the array name isn't the best - this isn't my original code but a dumbed down version I put together as an example. Yours is a much better name choice. Your AoA example looks like what I want - thanks for posting. Do you have any ideas on the second part - how to dynamically fill a table? Is the table::text module even capable of this? I added an output example to my original post for clarity. Thanks!

      Hi, is this what you are looking for:

      use strict; use warnings; use Data::Dumper; 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"; my @file_content = split( /\n/, $file ); my @full_data; push @full_data, [split] for @file_content; for my $line_ref (@full_data) { print "$_\t" for @$line_ref; print "\n"; }
      This is the output:
      $ perl table.pl 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

      Update Mar 11, 19:45 UTC: Looking again at the OP and at another answer you made which I had not seen, I think I now understand what you are trying to do (and the above is not what you need). You're basically wanting to do a form of transposition of the data, where, in part, what you have in row form should not appear in columns, and vice-versa, although it is not a simple matrix transposition. I don't have time right now (I'm gonna have dinner with my family right now), but I think I can come back to it in a couple of hours unless someone else does it before me.

        Now that what you really wanted is clear to me, I agree with 2teez that a hash of hashes is probably a better solution than the AoA one I suggested originally to represent your data not knowing what you wanted to do with it. I had warned in my first post that another data structure might be better, depending on what you intended to do with the data (which was not known at the time). And the very nice solution offered by Cristoforo seems to fit exactly what you need. So, although it would be perfectly possible to generate the same type of output with an AoA, there is no point for me to continue that route. Happy to help if you have any further questions.
      Hi Deep_Plaid, What do you mean by "how to dynamically fill a table"? Do you just want display (print) the data in tabular format? Is it something else?

        Hi LR. Sorry for not getting back to your sooner. I've been buried at work all week. I do appreciate the time you took to help, and I learned a lot by your examples. What I meant by dynamically fill a table is that the structure for the text::table module appears to be static (i.e., it works fine if your data doesn't change). However, the data I pass into this script will be constantly changing, thus adding rows to the table (i.e., the headings of the columns won't change, but the data in the table and the number of rows will change). I have some other responses using hash that I am going to try out now. Just wanted you to know I appreciate the help!