从哈希perl的哈希打印表

时间:2013-05-11 09:40:55

标签: perl

我有一个哈希,它有一个键'form'和Table的多个键指向其他哈希。但是,表格的任意数量为trtr将包含任意数量的td。所以问题是它有匿名数组。如何从这个哈希打印一个表?

'form'=>{
    'Table2' => {
    'tr' => [
      {
        'td' => [
                {
                  'db_feild_name' => 'project_details',
                  'type' => 'texta',
                  'display_name' => 'Project Detail',
                  'mandate' => '1'
                },
                {
                  'db_feild_name' => 'project_name',
                  'type' => 'text',
                  'display_name' => 'Project Name',
                  'mandate' => '1'
                },
                {
                  'db_feild_name' => 'project_date',
                  'type' => 'date',
                  'display_name' => 'Date'
                }
              ]
      },
      {
        'td' => [
                {
                  'db_feild_name' => 'issue_name',
                  'type' => 'text',
                  'display_name' => 'Name',
                  'mandate' => '1'
                },
                {
                  'db_feild_name' => 'last_name',
                  'type' => 'text',
                  'display_name' => 'last Name',
                  'mandate' => '1'
                },
                {
                  'db_feild_name' => 'age',
                  'type' => 'text',
                  'display_name' => 'Age'
                }
              ]
      }
    ],
    'Table_Header' => 'test_table2',
    'Title' => 'i am table'
    }
}

1 个答案:

答案 0 :(得分:0)

说明: $table指向,

{
  'tr' => [..],
  'Table_Header' => 'test_table2',
  'Title' => 'i am table'
}

$tr指向,

{
  'td' => [..]
},

$td指向

{
  'db_feild_name' => 'project_details',
  'type' => 'texta',
  'display_name' => 'Project Detail',
  'mandate' => '1'
},

假设您的哈希为%h

for my $table (values %{ $h{"form"} }) {
  # print <table>
  print $table->{"Table_Header"}, $table->{"Title"};

  for my $tr (@{ $table->{"tr"} }) {
    # print <tr>

    for my $td (values %$tr) {
      # print <td>
      print $td->{"db_feild_name"}; # type,display_name,etc
      # print </td>
    }

    # print </tr>
  }

  # print </table>
}