Okay, you posted code that has an obvious syntax error (elsf) and a fairly clear case where you misuse single-quotes ('$rowName'), you ask why it doesn't work, we mention these things, and you say "no, that's not the problem."

If you are running a program that compiles, runs and does something (even something wrong -- i.e. perl doesn't report a syntax error), then the code you are running is different in at least one crucial detail from the code that you posted. So you are showing us the wrong problem.

(Update: in case you didn't figure it out from your dialog with ikegami, when your IDE reports a syntax error, it's because perl reported a syntax error; the script won't run at all until you fix the syntax error.)

As for the single-quotes around $rowName when you use that as a hash key, that's just wrong. Don't do that. You can waste a little time putting double-qoutes around the variable if you really want to do that, but it will make no difference relative to using the variable with no quotes at all.

Try putting some debugging output like this:

warn "this_var=$this_var; that_var=$that_var\n";
at strategic points, and watch what comes out on STDERR. Or step through the process with "perl -d your_script_name", and use the debugger to place breakpoints and inspect variables in this troublesome subroutine.

Apart from all that, if the "table's last row number" is really just the number of rows in the table (which is how I would normally interpret the phrase), then what's wrong with doing this:

sub lastRowNum { my ( $dbh, $table ) = @_; my ( $rowcount ) = $dbh->selectrow_array( "select count(*) from $t +able" ); return $rowcount; }
Or, if "last row number" means something else, like "highest numeric index in the table's primary key field", then you could do something this:
sub lastRowNum { my ( $dbh, $key_fld, $table ) = @_; my ( $last_id ) = $dbh->selectrow_array( "select max($key_fld) fro +m $table" ); return $last_id; }
No extra module or subroutines required. Keep it simple. (Updated to structure these last two examples as complete subroutines.)

In reply to Re: Why isn't this subroutine working? by graff
in thread Why isn't this subroutine working? by northwestdev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.