7 Replies - 2009 Views - Last Post: 20 September 2011 - 01:28 PM Rate Topic: -----

#1 mossman65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-September 11

CFIF Issue

Posted 19 September 2011 - 10:59 AM

Hello Everyone!

I'm trying to figure out to do an if statement based on just the first 2 characters in my part number. We have 8 styles of parts. Each beginning with a number followed by a dash.

Here is what i have tried:

<cfif get_product_id.PartNumber eq "6-">
    Drag Race
<cfelseif get_product_id.PartNumber eq "7-">
    Muscle Car
</cfif>



Thank you very much!
Aaron

Is This A Good Question/Topic? 0
  • +

Replies To: CFIF Issue

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: CFIF Issue

Posted 19 September 2011 - 11:26 AM

Welcome to DIC mossman65!

Yeah, the CFIF you have there won't work. The reason for that is that the if statement is looking for an exact match, for instance, to "6-". Because "6-" isn't the same as "6-123" it won't ever come back as equal.

There's a couple of different ways you could do this depending upon your application. You COULD do the work in the database query itself wherein it returns the part style as part of the query return set. That's a little more complicated (although not all that much) and it's easier to do an inline check like you're trying to do here.

What I'd recommend is to try using either the FindNoCase or ListGetAt functions. Using the FindNoCase function, you're looking for the exact match to a string part...in your case, something like "6-". This should work but it does have one potential drawback: a FindNoCase for "6-" will return a true for "6-123" as well as for things like "56-123" and "1-456-123" and such because all of them have a "6-" in there somewhere. The ListGetAt function is a similar kind of search but in this case it treats the PartNumber as a list whose elements are separated by a dash...and then you'd be evaluating the first element. So, in this case it would look something like this:
<cfif ListGetAt(get_product_id.PartNumber,1,"-") EQ "6">
    Drag Race
<cfelseif ListGetAt(get_product_id.PartNumber,1,"-") EQ "7">
    Muscle Car
</cfif>



As in most things, there's usually more than one way to solve a problem. In fact, there's a way where you could use the ListGetAt function and then run the product of that through a CFSWITCH/CFCASE series as well...but what you have there should work fine for you.

Good luck!

This post has been edited by Craig328: 19 September 2011 - 11:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 mossman65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-September 11

Re: CFIF Issue

Posted 19 September 2011 - 11:29 AM

Thanks Craig!

View PostCraig328, on 19 September 2011 - 11:26 AM, said:

Welcome to DIC mossman65!

Yeah, the CFIF you have there won't work. The reason for that is that the if statement is looking for an exact match, for instance, to "6-". Because "6-" isn't the same as "6-123" it won't ever come back as equal.

There's a couple of different ways you could do this depending upon your application. You COULD do the work in the database query itself wherein it returns the part style as part of the query return set. That's a little more complicated (although not all that much) and it's easier to do an inline check like you're trying to do here.

What I'd recommend is to try using either the FindNoCase or ListGetAt functions. Using the FindNoCase function, you're looking for the exact match to a string part...in your case, something like "6-". This should work but it does have one potential drawback: a FindNoCase for "6-" will return a true for "6-123" as well as for things like "56-123" and "1-456-123" and such because all of them have a "6-" in there somewhere. The ListGetAt function is a similar kind of search but in this case it treats the PartNumber as a list whose elements are separated by a dash...and then you'd be evaluating the first element. So, in this case it would look something like this:
<cfif ListGetAt(get_product_id.PartNumber,1,"-") EQ "6">
    Drag Race
<cfelseif ListGetAt(get_product_id.PartNumber,1,"-") EQ "7">
    Muscle Car
</cfif>



As in most things, there's usually more than one way to solve a problem. In fact, there's a way where you could use the ListGetAt function and then run the product of that through a CFSWITCH/CFCASE series as well...but what you have there should work fine for you.

Good luck!

Was This Post Helpful? 0
  • +
  • -

#4 mossman65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-September 11

Re: CFIF Issue

Posted 19 September 2011 - 11:49 AM

Worked Great!!

View Postmossman65, on 19 September 2011 - 11:29 AM, said:

Thanks Craig!

View PostCraig328, on 19 September 2011 - 11:26 AM, said:

Welcome to DIC mossman65!

Yeah, the CFIF you have there won't work. The reason for that is that the if statement is looking for an exact match, for instance, to "6-". Because "6-" isn't the same as "6-123" it won't ever come back as equal.

There's a couple of different ways you could do this depending upon your application. You COULD do the work in the database query itself wherein it returns the part style as part of the query return set. That's a little more complicated (although not all that much) and it's easier to do an inline check like you're trying to do here.

What I'd recommend is to try using either the FindNoCase or ListGetAt functions. Using the FindNoCase function, you're looking for the exact match to a string part...in your case, something like "6-". This should work but it does have one potential drawback: a FindNoCase for "6-" will return a true for "6-123" as well as for things like "56-123" and "1-456-123" and such because all of them have a "6-" in there somewhere. The ListGetAt function is a similar kind of search but in this case it treats the PartNumber as a list whose elements are separated by a dash...and then you'd be evaluating the first element. So, in this case it would look something like this:
<cfif ListGetAt(get_product_id.PartNumber,1,"-") EQ "6">
    Drag Race
<cfelseif ListGetAt(get_product_id.PartNumber,1,"-") EQ "7">
    Muscle Car
</cfif>



As in most things, there's usually more than one way to solve a problem. In fact, there's a way where you could use the ListGetAt function and then run the product of that through a CFSWITCH/CFCASE series as well...but what you have there should work fine for you.

Good luck!

Was This Post Helpful? 0
  • +
  • -

#5 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: CFIF Issue

Posted 19 September 2011 - 11:51 AM

Good to hear. :)
Was This Post Helpful? 0
  • +
  • -

#6 mossman65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-September 11

Re: CFIF Issue

Posted 20 September 2011 - 12:36 PM

Hello Craig,

Thank you again for your help!

I have another question if you have a moment.

The ListGetAt function seems to be the answer to many finishing touches to our data, and i want to use it in more ways.

I'm trying to use the ListGetAt function like so:
<cfif ListGetAt(get_product_id.PartNumber,3,"-") EQ "2">
	1 INCH TUBE
<cfelseif ListGetAt(get_product_id.PartNumber,3,"-") EQ "4">                                
	1.25 INCH
<cfelseif ListGetAt(get_product_id.PartNumber,3,"-") EQ "5">                                
	1.5 INCH
</cfif>



Part Number Example: 1-258BW-FAX

I did look at a few examples and at adobe docs to see if i could figure it out but i'm still a little confused. If in the function i set the list "get_product_id.PartNumber" and then the then the position of what i want examined 3 so that it jumps just past the dash. Is the "-" the right delimiter in this case like the other one? Could you tell me why the delimiter is needed, or why i couldn't just get a count from the left to the position that i want?

I assumed that i could just use the same code to find the first digit of the part number after the dash. The digit after the dash represents the size tubing we used. What i did above doesn’t error but no results are displayed.



View PostCraig328, on 19 September 2011 - 11:26 AM, said:

Welcome to DIC mossman65!

Yeah, the CFIF you have there won't work. The reason for that is that the if statement is looking for an exact match, for instance, to "6-". Because "6-" isn't the same as "6-123" it won't ever come back as equal.

There's a couple of different ways you could do this depending upon your application. You COULD do the work in the database query itself wherein it returns the part style as part of the query return set. That's a little more complicated (although not all that much) and it's easier to do an inline check like you're trying to do here.

What I'd recommend is to try using either the FindNoCase or ListGetAt functions. Using the FindNoCase function, you're looking for the exact match to a string part...in your case, something like "6-". This should work but it does have one potential drawback: a FindNoCase for "6-" will return a true for "6-123" as well as for things like "56-123" and "1-456-123" and such because all of them have a "6-" in there somewhere. The ListGetAt function is a similar kind of search but in this case it treats the PartNumber as a list whose elements are separated by a dash...and then you'd be evaluating the first element. So, in this case it would look something like this:
<cfif ListGetAt(get_product_id.PartNumber,1,"-") EQ "6">
    Drag Race
<cfelseif ListGetAt(get_product_id.PartNumber,1,"-") EQ "7">
    Muscle Car
</cfif>



As in most things, there's usually more than one way to solve a problem. In fact, there's a way where you could use the ListGetAt function and then run the product of that through a CFSWITCH/CFCASE series as well...but what you have there should work fine for you.

Good luck!

Was This Post Helpful? 0
  • +
  • -

#7 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1926
  • View blog
  • Posts: 3,471
  • Joined: 13-January 08

Re: CFIF Issue

Posted 20 September 2011 - 01:01 PM

Hey, no problem. I can explain what you're seeing and why. Let's use part number as the basis for the following examples (1-258BW-FAX):

For a line of code like this...
<cfif ListGetAt(get_product_id.PartNumber,3,"-") EQ "2">
...you actually have two things going on. Let's start with the IF part. The CFIF tag essentially evaluates two values and tells you whether they are equal or not. In the example above, you want to know if something (the second thing going on) is equal to "2". To understand why it's not evaluating to "2" you need to understand what the second part of that line of code (the ListGetAt function) is doing.

Am easy way to see it would be to take and run this piece of code:
<cfset variables.PartNumber = "1-258BW-FAX"><cfoutput>#ListGetAt(variables.PartNumber,3,"-")#</cfoutput>

What you should see should be "258BW". That's because the ListGetAt function is being told "for the list that is '1-258BW-FAX', return the second element of that list where the elements are separated by dashes". In this case, that element is indeed "258BW" and since that is so, "258BW" is not the same as "2" so the CFIF does not evaluate them as being equal.

All is not lost though. The ListGetAt function did manage to isolate the element (258BW) so what you want to know now is "what is the first character of that element", right? In this case, because you want to know about the first or leftmost character in that newly isolated string element, you need to use the function called Left. The Left function is quite simple in that it will parse a string and return the leftmost X characters that you tell it to. In this case, your complete IF statement line would look like this:
<cfif Left(ListGetAt(get_product_id.PartNumber,3,"-"),1) EQ "2">

Understand that just like math, Coldfusion functions evaluation order is from left to right and from inside out. So, in that code line the first thing it does is the ListGetAt function and then it goes to the Left function and finally it does the CFIF evaluation.

It looks a little complex if you're not used to it but that line should do what you want it to do and you can copy that format to the cfelseif statements and it should work fine.
Was This Post Helpful? 1
  • +
  • -

#8 mossman65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 19-September 11

Re: CFIF Issue

Posted 20 September 2011 - 01:28 PM

Man thank you so much for explaining it to me, i thought it was just counting left to right :) That makes complete sense now. I have used IF statements quite a bit, but this is moving in to the advanced realm for me. I look forward to learning more!

Best Regards,
Aaron

View PostCraig328, on 20 September 2011 - 01:01 PM, said:

Hey, no problem. I can explain what you're seeing and why. Let's use part number as the basis for the following examples (1-258BW-FAX):

For a line of code like this...
<cfif ListGetAt(get_product_id.PartNumber,3,"-") EQ "2">
...you actually have two things going on. Let's start with the IF part. The CFIF tag essentially evaluates two values and tells you whether they are equal or not. In the example above, you want to know if something (the second thing going on) is equal to "2". To understand why it's not evaluating to "2" you need to understand what the second part of that line of code (the ListGetAt function) is doing.

Am easy way to see it would be to take and run this piece of code:
<cfset variables.PartNumber = "1-258BW-FAX"><cfoutput>#ListGetAt(variables.PartNumber,3,"-")#</cfoutput>

What you should see should be "258BW". That's because the ListGetAt function is being told "for the list that is '1-258BW-FAX', return the second element of that list where the elements are separated by dashes". In this case, that element is indeed "258BW" and since that is so, "258BW" is not the same as "2" so the CFIF does not evaluate them as being equal.

All is not lost though. The ListGetAt function did manage to isolate the element (258BW) so what you want to know now is "what is the first character of that element", right? In this case, because you want to know about the first or leftmost character in that newly isolated string element, you need to use the function called Left. The Left function is quite simple in that it will parse a string and return the leftmost X characters that you tell it to. In this case, your complete IF statement line would look like this:
<cfif Left(ListGetAt(get_product_id.PartNumber,3,"-"),1) EQ "2">

Understand that just like math, Coldfusion functions evaluation order is from left to right and from inside out. So, in that code line the first thing it does is the ListGetAt function and then it goes to the Left function and finally it does the CFIF evaluation.

It looks a little complex if you're not used to it but that line should do what you want it to do and you can copy that format to the cfelseif statements and it should work fine.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1