我已经尝试了各种@%$的组合,但我无法使其正常工作。整个表达式可以工作,但是我该如何将其拆分为变量?
print "BIGexp ", $tables{"grp"}[1]{_name}, "\n"; # OK, prints part
my @cols = $tables{"grp"};
print "COLS ", @cols, "\n"; # prints array
my %col = %cols[1];
print "COLvar ", %col, " ", "\n"; #prints 1 ?
print "COLexp ", $cols[1]{_name}, "\n"; ######## Fails, uninitialized.
我想表示数据库模式。因此,需要一个表格映射,每个表格包含一个列数组,每个列都是一个映射。以下是完整片段。
my %tables=();
my $columns; # array
sub addTable {
my @cols = ();
$tables{$_[0]} = \@cols;
$columns = \@cols;
}
sub addColumn {
my %col = (_name => $_[0], _typ => $_[1]);
push(@$columns , \%col);
}
addTable("grp");
addColumn("id", "serial");
addColumn("part", "integer");
addTable("tab2");
addColumn("foo2", "varchar");
addColumn("bar2", "integer");
print "BIGexp ", $tables{"grp"}[1]{_name}, "\n"; # OK, prints part
my @cols = $tables{"grp"};
print "COLS ", @cols, "\n"; # prints array
my %col = %cols[1];
print "COLvar ", %col, " ", "\n"; #prints 1 ?
print "COLexp ", $cols[1]{_name}, "\n"; ######## Fails, uninitialized.
我是 Perl 的新手,还没有完全理解 Perl 的思维方式 :(. 非常感谢任何帮助。
my %col = %{$cols->[1]}
替换代码行 "my %col = %cols[1];" - Håkon Hægland