in reply to Type casting

Well, there's always
$year=int($year);

But Perl is not a strongly-typed language. You can also use
$year+=0;

to ensure that $year is interpreted as a number in following statements (as long as the following statements don't do anything to the variable).

However, the code you show should make year a number in any case. Perhaps the code actually is
$year = '1992';

or is otherwise assigned a value interpreted as a string.

Replies are listed 'Best First'.
Re: Re: Type casting
by Anonymous Monk on Jul 18, 2001 at 03:38 UTC
    Hey, Thanks but it isnt working....can look at my code..
    use Win32::ODBC; my @rows; my $make1="Acura"; $year=int($year); # $year+=0; $year=1992; my $DSN = "carcov"; if (!($db = new Win32::ODBC($DSN))){ print "error connecting to $DSN\n"; print "error: " . Win32::ODBC::Error() . "\n"; exit; } die qq(SQL failed: ), $db->Error(), qq(\n) if ($db->Sql("SE +LECT DISTINCT modelname,startyear,endyear FROM model +,detail,make WHERE detail.modelid=model.mod +elid AND detail.makeid=make.makeid AND make.makename='$make1' AND startyear <= '$year' AND endyear >= '$year'"));

      First, print you SQL statement to check it looks OK.

      Then try it with less requirements to see whether you're asking for an impossible dataset.

      Then ummm... I'll get back to you...

      clive ;-)

      Although you want $year to be treated by the database as a number, you've put single quotes around it in the SQL, which tells the database the value is a string.

      Try removing the single quotes around $year in the SQL, and maybe even using placeholders instead, if MS Access supports them.

      Well, there are some notes above that may make this irrelevant, but you are doing the assignement after the $year+=0;. When trying to force evaluation as a number, you should do the +=0 trick after, not before the assignment.
      From this, it looks like your SQL string contains carrige returns. I don't belive you can do that.

      If that's not the case, what happens if you just replace all occurances of $year with a hard coded int?

      Rich

      Update: Thanks to crazyinsomniac for correcting me. SQL is not delimited by a CR so I was completely off base.

      Type really shouldn't make a difference here, since, when it's passed to the db, it's all a string anyway. But to make sure, you can always just drop $year and add 1992 in the code and see what the return is. I'm betting it will be the same result, in which case you will have to look elsewhere for the flaw.
      -Syn0

      update: damn, rchiav beat me :) .. another thing, when forcing the type, you dont' want to do it before you declare what's in $year, you want to do it during or after.
      for example: $year = 1992; $year = int($year); or $year = 1992+0;