I have a table with amongst other things a code column. I then have a Perl script that looks into the code column and automatically produce a new code that is simply the last code in the database +1.

The snippet of code in Perl is as follows:

while (@vresult=$v->fetchrow())

{ for($index=0;$index<@vresult;$index++)

{$c=$vresult[$index];

}

}

$code=$code+1

@vresult is the result of a database query to get the code out of the database.

it works fine when I have code 1-9 in the database but as soon as I have code above 10 then the code doesn't work anymore, it keeps wanting to print $code=10.

I realised that is because it considers 9 as being a higher number than 10. So my question is how can I improve this perl code so that if in the databse I have a code 11 the $code will become 12 and not 9?

Mmm. What data type is you column for 'code'? The way you are describing it, it sounds like it is not an integer. I would recommend changing it to an integer and then most of your problems will go away. On another note: is this code column the primary key for your table? If it is you should make it an auto_increment field too. That way MySQL will do all the incrementing for you, so you never have to set this value in your perl code.

If these suggestions are not an option, then try this code:

Code

#-- Grab the highest value of 'code' in the database my ($code) = $dbh->selectrow_array("SELECT MAX(CAST(code AS SIGNED)) FROM your_table"); #-- Add one to it $code += 1;