I need help in writing the correct Switch statement in CFScript.
Is this the right way?
I'm not sure with the Len(DRCode), should I put " " ot just Len(DRCode)
The case is using ""
Digits="Len(DRCode)";
switch(Digits)
{
case "6":
{
getOrderNo= "0" & DRCode;
break;
}
case "7":
{
getOrderNo= DRCode;
break;
}
}
Ian Skinner - 31 Jul 2007 18:43 GMT
> I need help in writing the correct Switch statement in CFScript.
> Is this the right way?
[quoted text clipped - 15 lines]
> }
> }
No quotes.
switch(len(DRCode))
....
OR
Digits = len(DRCode);
switch(Digits)
....
Otherwise you are comparing the string 'len(DRCode)' to the string '6'
and string '7' which is never going to match. You want to compare the
result of the len(DRCode) function to the strings '6' and '7'.
That is why the case statements use quotes. In its heart, a switch
comparison is a string comparison. Keep this in mind, because it can
have subtle implications to ones comparison logic.
Azadi - 31 Jul 2007 18:45 GMT
have tested your code? what happened?
no quotes. with quotes you are setting your var to equal literal string
"Len(DRCode)", not the value of expression len(DRCode).
and you can just do switch(len(drcode)) if you are not using your Digits
var anywhere else in your script
---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
cf_dev2 - 31 Jul 2007 19:01 GMT
> Digits="Len(DRCode)";
Don't enclose it in quotes. That will prevent CF from evaluating the Len()
function. Try
<cfscript>
switch(Len(DRCode)){
case "6":
getOrderNo= "0" & DRCode;
break;
case "7":
getOrderNo= DRCode;
break;
}
</cfscript>
Or if you're trying to pad the string with zeroes, try something like
<cfset getOrderNo = right("0000000"& DRCode, 7)>
Ian Skinner - 31 Jul 2007 19:19 GMT
> Or if you're trying to pad the string with zeroes, try something like
>
> <cfset getOrderNo = right("0000000"& DRCode, 7)>
Or something I've done in the past, but may not be as elegant as
cf_dev's. Mine only has the slight advantage of if the string length
changes there is one value to change, but it does require two function
calls instead of one, so they both have worthy points.
<cfset getOrderNo = replace(rJustify(DRCode,7)," ","0","ALL").
alecken - 31 Jul 2007 20:39 GMT
The problem when I did your suggestions, I got an error saying the variable
getOrderNo is undefined.
<cfscript>
switch(Len(DRCode)){
case "6":
getOrderNo= "0" & DRCode;
break;
case "7":
getOrderNo= DRCode;
break;
}
</cfscript>
I thought i can't use CF tags within cfscript, like: <cfset... ?
Azadi - 31 Jul 2007 20:52 GMT
1) check your syntax. i think cf_dev2 has omitted a couple of { } there
and ou just copied the code over...
2) yes, you can't use cf tags inside cfscript. but with cf_dev2's second
option you will not need cfscript at all.
---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
cf_dev2 - 31 Jul 2007 21:17 GMT
> The problem when I did your suggestions, I got an error saying the variable
> getOrderNo is undefined.
The problem is likely that your switch statement doesn't have a [b]default[/b]
case. So if the DRCode length is NOT 6 or 7 the "getOrderNo" variable is never
set.
> I thought i can't use CF tags within cfscript, like: <cfset... ?
You can't. The <cfset ..> statement was intended to replace your switch/case
block. Either use it outside the cfscript block or rewrite it in cfscript
syntax.
alecken - 31 Jul 2007 21:07 GMT
No, I did not copied the code over, Below is my actual codes:
The error I'm getting is: getOrderNo is undefined.
if (attributes.TypeID neq 0) {
switch(Len(DRCode))
{
case "6":
{
getOrderNo= "0" & DRCode;
break;
}
case "7":
{
getOrderNo= DRCode;
break;
}
}
strB = #s_recipient.CensusZIP# & getOrderNo & 0000;
}
cf_dev2 - 31 Jul 2007 21:31 GMT
Yes, Azadi's right. I did omit some of the {} . But it shouldn't cause a
syntax error. I don't think they're required for each [i]case[/i]
Check the DRCode value. I suspect its length is something other than 6 or 7
and that's why getOrderNo is not defined.