1

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.

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 ?

This is my code:

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();

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

[EDIT]

$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'
          ]
        ];
achahbar
  • 901
  • 3
  • 21
  • 47
  • 1
    You are mixing up strings and data structures. Take a look at what is in your `$databases`. Do the following; `use Data::Dumper; my $databases = $SQL->fetchall_arrayref(); print Dumper $databases;` and then [edit] your question and add the output please. – simbabque May 04 '18 at 12:12
  • Ok, so you have got an array reference that holds several array references. Now what exactly do you want to print out? – simbabque May 04 '18 at 12:19
  • I had/still have this in my mind. That this is the value of the array postgres1078556kb test107529kb each line being an output of the select statement , with independent values on 1 row. and inside the loop over the rows i have too loop through the rows to get the values – achahbar May 04 '18 at 12:22
  • This maybe sounds strange but by typing my explenation i found a solution and i tried it out and it works. :D – achahbar May 04 '18 at 12:23
  • 2
    That's not strange at all. That's called learning. It often helps to explain things to someone to understand them yourself. Well done! :) – simbabque May 04 '18 at 12:23
  • 1
    Feel free to write up your solution as your own answer. You can accept that answer tomorrow. Don't put it into the question though please. – simbabque May 04 '18 at 12:24

1 Answers1

2

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

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;
            }

        }
achahbar
  • 901
  • 3
  • 21
  • 47
  • 2
    You might also want to read http://perldoc.perl.org/perldsc.html and http://perldoc.perl.org/perlreftut.html – simbabque May 04 '18 at 12:32