1

我有一个 perl 脚本,它连接到 postgres 数据库并获取它在其上运行的几个选择语句。此输出的目的是在单词后记中打印。当我在构建我的项目时,我没有注意到获取部分发生了什么,在我用 word 打印它之后,我发现了一些奇怪的东西。

我执行一个 select 语句并在 arrayref 中获取它,但是当我打印这个 arrayreferentie 时,我的数组中不仅有值,而且还有空字符串,de cli 给了我一些错误代码。我首先想在我读完一行之后就咀嚼,这样它就会转到下一行,但这没有用。有没有人已经解决了这个问题?

这是我的代码:

my $query = $_[0];    #select multiple columns
my $SQL = $dbh->prepare($query);  
$SQL -> execute();

my $headers = $SQL->{NAME};
my $databases = $SQL->fetchall_arrayref();

#testing fase , print the fetch to see what would be print in word
foreach my $row (@$databases)
    {

        my $databaseColumnNumber =0;

        while ($databaseColumnNumber <= @$databases)
        {
            print "\n" , $row -> [$databaseColumnNumber];
             $databaseColumnNumber++;
        }
            #chomp $row;   ### this was a testing line to see if it would chomp after the empty value
    }



$SQL->finish();

我的 CLI 中的打印输出如下

postgres 
10 
7856 kB
//emptyline(which represents the empty value in the array
 Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
 Use of uninitialized value in print at
  Use of uninitialized value in print atC:\Users\xxxxx
//newline from the print to go to the new line in the array
test 
10 
7529 kB 
//emptyline(which represents the empty value in the array
 Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
 Use of uninitialized value in print at
  Use of uninitialized value in print atC:\Users\xxxxx

[编辑]

$VAR1 = [
          [
            'postgres',
            '10',
            '7856 kB'
          ],
          [
            'test',
            '10',
            '7529 kB'
          ],
          [
            'template1',
            '10',
            '6865 kB'
          ],
          [
            'template0',
            '10',
            '6865 kB'
          ]
        ];
$VAR1 = [
          [
            'city',
            '408 kB',
            '152 kB'
          ],
          [
            'countrylanguage',
            '136 kB',
            '88 kB'
          ],
          [
            'country',
            '96 kB',
            '56 kB'
          ]
        ];
4

1 回答 1

2

我为未来的读者找到了我自己的问题的解决方案:

我的 2 个循环没有正确完成(我使用了一个 foreach 和一段时间),下面的代码完成了这项工作。

 foreach my $row (@$databases)
        {

            my $databaseColumnNumber =0;
             foreach my $val (@$row) 
             {
                print "\n" , $val;
            }

        }
于 2018-05-04T12:25:36.370 回答