Perl dbi (select) выбирает пустые строки в массиве
У меня есть Perl-скрипт, который подключается к postgres db и выбирает пару операторов select, которые он запускает на нем. Этот вывод имеет целью напечатать слово послесловие. Пока я строил свой проект, я не заметил, что происходит с извлекаемой частью, и только после того, как я напечатал это словом, я заметил что-то странное.
Я выполняю оператор select и извлекаю его в arrayref, но когда я печатаю эту ссылку, я получаю не только значения в моем массиве, но и пустые строки, и 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'
]
];
1 ответ
Я нашел решение своего собственного вопроса для будущих читателей:
Мои 2 цикла были выполнены неправильно (я использовал foreach и некоторое время), следующий код сделал свою работу.
foreach my $row (@$databases)
{
my $databaseColumnNumber =0;
foreach my $val (@$row)
{
print "\n" , $val;
}
}