Rocko19 has asked for the wisdom of the Perl Monks concerning the following question:


Hi,
I am trying to fetch some records from DB views and trying to write them to a text file. When I run the code I get the error
"Can't use string<""> as a HASH ref while "strict refs" in use at dbcsv.pl line 52".

I know there is some problem somewhere in fetching the rows. Can some one tell me the correct way to fetch the records so they get written also in the text file?

Thanks
Rocko
  • Comment on How to Fetch records from Oracle DB view

Replies are listed 'Best First'.
Re: How to Fetch records from Oracle DB view
by kennethk (Abbot) on Sep 03, 2009 at 14:53 UTC
    My guess is that line 52 corresponds to

    foreach my $m(0..$#row1)

    If not, you should indicate since you haven't given us 52 lines of code. In the above, $#row1 is the last index of @row1, which you don't define. Since you want the last index of the array referenced by $row1, you mean

    foreach my $m(0..$#$row1)

    Note the extra sigil (and see perlref and/or perlreftut). A less error prone way of writing that block assuming you don't need the index would be using a foreach loop on the array itself:

    foreach my $record (@$row1) { $data_file.= $record->{'ROW_DATA'} . "\n"; }

    Update: You are also accessing the array reference improperly in the line

    $data_file.= "$row1[$m]{'ROW_DATA'}\n";

    which should be

    $data_file.= "$row1->[$m]{'ROW_DATA'}\n";

    since you need to dereference that reference to access its contents. You should probably (re)read perlreftut to gain insight into how references work in Perl.

      I tried the code but again got the error - "Not a Hash reference at dbcsv.pl line 58" where line 58 is. Please help.

        It can't work -- compare your piece of code with the one kennethk posted.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Another issue I missed - you've used fetchall_arrayref() (see DBI) and then attempted to access the results as a hash. Part of the issue is of course that I can't run your script to test it since I don't have access to your database so I can't debug. I can only guess that your code through the execute is functioning correctly. I do note that you are mixing hash and array access concepts in accessing the results. As per the documentation,

        The fetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row.

        So based on what I'm reading, you first need to modify your SQL to only select the column you are looking for (ROW_DATA presumably) and then access the results like an array reference:

        foreach my $record (@$row1) { $report.= "$row1->[0]\n"; }

        Alternatively, you could change the 0 in the above to reflect the appropriate column for what you are trying to do. Please read perlreftut, DBI and a review of Perl variable types might not be a bad idea. As well, please read the error messages and consider what they say.