2015-08-28 70 views
-1

我想製作一個程序,要求用戶輸入一些信息(我保存在一個數組中),然後創建一個表格作爲列名稱,但我不知道如何動態創建列。使用Text :: Table創建具有動態列數的表格

我目前使用的是Text::Table模塊,用這個模塊實現這一點會很好。

這是一個例子陣列,我會從用戶輸入得到:

my @columns = ("TIME", "P1", "P2", "B1", "P3"); 

這就是我正在做手工表:

my $table = Text::Table->new(
    {title => 'TIME', align_title => 'center'}, 
    {title => ' P1 ', align_title => 'center'}, 
    {title => ' P2 ', align_title => 'center'}, 
    {title => ' B1 ', align_title => 'center'}, 
    {title => ' P3 ', align_title => 'center'} 
); 
+0

你能後到目前爲止你有什麼? –

回答

2

你有一個字符串數組;將其轉換爲hashrefs的數組,其中每個hashref是column spec你可以使用map

use Text::Table; 

my @columns = ("TIME", "P1", "P2", "B1", "P3"); 
my @col_spec = map { { title => $_, align_title => 'center' } } @columns; 

my $table = Text::Table->new(@col_spec); 
+0

是否可以更改標題屬性,例如對齊? – Summoner1337

+0

@ Summoner1337當然可以。我已經更新了我的答案。 – ThisSuitIsBlackNot