Perl DBI(选择)获取数组中的Empy字符串 [英] Perl dbi (select) fetches empy strings in array

查看:82
本文介绍了Perl DBI(选择)获取数组中的Empy字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个perl脚本,该脚本连接到postgres db并获取在其上运行的几个select语句。此输出的目的是在单词后单词中打印。在构建项目时,我没有注意到提取部分发生了什么事情,只是在我用文字打印出来之后,我发现了一些奇怪的东西。

I have a perl script that connect to a postgres db and fetches a couple select statements that it runs on it. This output have the purpose to print in word afterword. While i was building my project i didn't notice something that happens with the fetching part ,it was after i print it in word that i noticed something strange.

我执行一条select语句,并在arrayref中获取它,但是当我打印此arrayreferentie时,我不仅在数组中没有值,而且没有空字符串和de cli给我一些错误代码。首先,我在读完一行之后只是断断续续,以便可以转到下一行,但这没有用。

I execute a select statement and fetches this in an arrayref , but when i print this arrayreferentie I don't got only values in my array but also empty strings and de cli gives me some error code with it. I firstly though to just chomp after i read a line so that it wil go to the next line but that didn't work . Is there anyone who have come to this problem already ?

这是我的代码吗?

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中打印的输出如下所示

The output of the print in my CLI is as followed

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'
          ]
        ];


推荐答案

我为自己的问题找到了解决方案,以备将来使用读者:

I found the solution to my own question , for future readers:

我的2个循环未正确完成(我使用了foreach一段时间),以下代码完成了工作。

My 2 loops where not correctly done (I used a foreach and a while) the following code did the job.

 foreach my $row (@$databases)
        {

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

        }

这篇关于Perl DBI(选择)获取数组中的Empy字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆