如何在Perl中处理postgres数组?

时间:2016-03-21 22:36:00

标签: arrays postgresql perl

我是Perl的新人。我试图使用Perl脚本在postgres视图中选择一个数组列,但是我不确定如何在将Perl插入Perl数组时打破标量数组,以便我可以单步执行每个领域。

您可以提供什么建议?

(使用postgres 9.3,相对较新的Perl)

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
# open database connection
my $dbh = DBI- >connect("dbi:Pg:dbname=$db::DB_name;host=$db::DB_host;port=$db::DB_port","$db::DB_user","$db::DB_pwd");
if ( !defined $dbh ) {&log_print("Cannot connect to database!\n"); die"dieing...\n";} 
#fetch record from postgres database
$sth = $dbh->prepare("select progno, track_numbers from processing");
if ( !defined $sth ) {    die "Cannot prepare statement: $DBI::errstr\n";} 
$rows = $sth->execute();
my ($p_progno, $p_track_numbers) = $sth->fetchrow();
$sth->finish;
#output scalar
&log_print("$p_track_numbers\n"); 
#result: ARRAY(0x1975d38)
print Dumper($p_track_numbers);
#result:
#$VAR1 = [
#          16,
#          17,
#          18,
#          19,
#          20,
#          21,
#          22
#        ];
#? How do I step through each $p_track_numbers in a foreach?

1 个答案:

答案 0 :(得分:0)

通过添加@

来取消引用数组引用
for my $track_number (@$p_track_numbers) {
    print $track_number, "\n"
}