Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Getting table dimensions

by steelrose (Scribe)
on Jun 20, 2005 at 13:32 UTC ( #468337=perlquestion: print w/ replies, xml ) Need Help??
steelrose has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Wise Monks: I have a program that in part uses a database object to query a database and return it's contents in a table to the originating program.

The problem I'm having is that I then want to loop through all of the rows in that table, which of course can vary. The code I was attempting to use causes a "Can't use string ("2") as an ARRAY ref while "strict refs" in use at..." error.

2 is of course, the correct number of rows in my test scenario. What I'm not grasping is why if perl knows the value, is it not able to insert it into the variable?

Any suggestions are helpful.
my $Table = $dbObject->getTable; my $tableDimensions = @$Table; my $numRows = $tableDimensions->[0]; #the error occurs here for (my $timesThru=0; $timesThru<=$numRows; $timesThru++) {
If you give a man a fish he will eat for a day.
If you teach a man to fish he will buy an ugly hat.
If you talk about fish to a starving man, you're a consultant.

Comment on Getting table dimensions
Download Code
Re: Getting table dimensions
by dragonchild (Archbishop) on Jun 20, 2005 at 13:36 UTC
    Your snippet is not complete - it doesn't show us how @arrayDimensions was set.

    I think you're having issues with what you want your code to do. Please explain that, in short English sentences without any code. I think that if you do that, you'll solve your problem using us as a teddybear.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Sorry, arrayDimensions was a typo. I've corrected it in the original post.

      Simply put I want to have a variable which contains the number of rows in a table of data extracted by a database Object.
      If you give a man a fish he will eat for a day.
      If you teach a man to fish he will buy an ugly hat.
      If you talk about fish to a starving man, you're a consultant.
Re: Getting table dimensions
by aukjan (Friar) on Jun 20, 2005 at 13:39 UTC
    if you want a loop through all the variables of an array you can do:
    @array = qw( 1 2 3 4 5 6); for $array_element ( @array ){ print "This is the element: $array_element\n"; }
    or if you want to use the index:
    @array = qw( a b c d e f); for $i ( 0 .. $#array ){ print "This is the $i th element: ". $array[$i] ."\n"; }
    Is this what you are looking for?

    .:| If it can't be fixed .. Don't break it |:.

Re: Getting table dimensions
by trammell (Priest) on Jun 20, 2005 at 14:18 UTC
    You say:
    my $tableDimensions = @$futureTable; my $numRows = $tableDimensions->[0];
    Assuming that $futureTable is an arrayref, then $tableDimensions contains the number of elements in that array, an integer. So what do you expect to happen when you dereference an integer?
      I knew it was something silly I was overlooking. Thanks.

      If you give a man a fish he will eat for a day.
      If you teach a man to fish he will buy an ugly hat.
      If you talk about fish to a starving man, you're a consultant.
Re: Getting table dimensions
by jasonk (Parson) on Jun 20, 2005 at 14:20 UTC

    Your problem starts with my $tableDimensions = @$futureTable;, by dereferencing the arrayref, and assigning it to a scalar, $tableDimensions now contains the number of elements in the array, so that when you call my $numRows = $tableDimensions->[0]; you try to dereference an integer. It looks like what you actually intended to do was to determine the number of rows that were returned, and you've added an extra step. If I'm reading your code right you actually just need this:

    my $Table = $dbObject->getTable; my $numRows = @$futureTable; # this is a more compact way to write the same for loop for my $timesThru (0 .. $numRows) {

    We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://468337]
Approved by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-07-22 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (126 votes), past polls