ColdFusion | Split Database String - coldfusion

I have the following:
<dd>
<select id="contactLocation" name="contactLocation" size="1">
<option value="Online"<cfif attributes.contactLocation eq "Online">selected</cfif>>Online</option>
<cfoutput query="storeLocations">
<option value="#storeLocations.name#"<cfif attributes.contactLocation eq "#storeLocations.name#">selected</cfif>>#storeLocations.state# - #storeLocations.city#, #left(storeLocations.storeID, 3)#</option>
</cfoutput>
</select>
I added the two top cfset variables as I was trying to figure it out. The string in the database returns 111/NAME and I want to remove the forward slash and everything else to the right of it.
Currently with the two variables I added it just returns the first 3 characters but it is only displaying the one row for every single item in the drop down.
How do I remove the forward slash and everything to the right of it within #storeLocations.storeID#
UPDATE:
Actually Now I have it display all 3 characters per row but what if there are only 2 or if there are 4? I am close I just need the last part.

If the separator is ALWAYS a "/" character and the "/" character will not appear in either token, think of this value as a list with a "/" delimiter and just get the last element with listLast:
listLast(storeLocations.storeID, "/")
If the 2nd part could have a "/" in it, but the first part never will, because its a number, then you can just get rid of the first token using listRest:
listRest(storeLocations.storeID, "/")
Edit: Since I can't tell my right from my left...
listFirst(storeLocations.storeID, "/")

Related

Inserting Characters into NetSuite Saved Search Results Column

I'm trying to find a way to insert line breaks into certain places in a text area column in a NetSuite saved search. In some instances this "comments" value contains data separated by line breaks with each line started by a digit followed by a period. The issue is that when this data is presented to be included as part of the results, the line breaks are removed, causing everything to run together. For the most part, I've been able to at least find where I need to insert the line breaks with the following:
REGEXP_REPLACE({notes}, '\d{1,2}\.', '<br />', 3, 0, 'i')
The problem, though, is that replaces all of the numerical "bullet points" except for what may be at the very first (typically '1.'), which is not desirable. Is there a way to re-insert these line breaks and keep the numerical bullets?
You can re-use the matched text in the replacement pattern through backreferences:
REGEXP_REPLACE({notes}, '(\d{1,2}\.)', '<br />\1', 3, 0, NULL)
This will start searching for a match, case-sensitively (note I replaced 'i' with NULL since digits and a dot char are casefree chars), from the third position in the string, and will replace all matches (due to the 0 occurrence argument) with the same text as matched with <br /> prepended to it.

regexp_replace() - matches but does not replace at end of line

I'm trying to regexp_replace() all the values of a column ending without "/", by adding "/".
I can get the correct values by using this statement (the pattern was tested with a PCRE checker):
SELECT * FROM `table` WHERE `column` REGEXP("(?<=[^\/])$");
And the non-matching ones with:
SELECT * FROM `table` WHERE `column` REGEXP("(?<![^\/])$");
But when the statement is:
UPDATE `table` SET `column` = REGEXP_REPLACE(`column`, "(?<=[^\/])$", "/");
Then, there is no change, whatever value I put into the third parameter:
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1031 Changed: 0 Warnings: 0
You could do this easily without regex:
UPDATE `table` SET `column` = `column` + '/'
WHERE RIGHT(`column`, 1) <> '/'
trying to understand why it does not work
As I rationalize the problem, you are asking REGEXP_REPLACE to do two things:
Discover that something is missing, and
Point to a location in the string.
Your regexp says that it is missing, but I question whether it points to a specific substring (even an empty one) for replacing. It's easy to point to a found substring (or substrings). It is hard to point to a missing substring. And such a 'pointer' is needed to do the replacement.
Hence, Michal's approach (even if some regexp were needed) is the "right" way to solve the problem.

coldfusion - remove specific text from string

I am looking to remove "pharmacy" from an input field before I enter the text into the database.
Example - "Ciaran pharmacy" will show as "Ciaran".
Your requirements are pretty simple:
<cfscript>
name = "Ciaran pharmacy";
newName = replace(name, 'pharmacy', '');
</cfscript>
I suspect there's more complication to it than that.
Is it only if 'pharmacy' is the last word?
Is it always just the last word in the name?
Or is it always 'pharmacy' regardless of where it is in the string?
If it's the 3rd case, specify the 'ALL' parameter:
newName = replace(name, 'pharmacy', '', 'ALL');
You can also use REReplace to remove multiple occurances of the string. And TRIM function to remove the spaces before and after.
<cfoutput>
#TRIM(REReplace("pharmacy in Ciaran pharmacy", "pharmacy", "","ALL"))#
</cfoutput>
local.replaceContent = replace('My Name is developer', 'My', 'I','ALL');
writeDump(local.replaceContent);
abort;
Result

perform substring extraction on data frame column

I have a dataframe with 1 column called 'full_url'. Each element of the column is just a url. How to I write a function to remove the 'http://' from all of the elements at once? I need to use some kind of regex because some don't have it at all, some have https, etc. The closest I've gotten is gsub(".*//","",unlist(full_url))
but that also returns 'full_url1' 'full_url2' 'full_url3' ... as the row names for some reason
Without a reproducible example I'm not sure, but would something like this work?
apply(df$full_url, 1, function(x) ifelse(substr(x,1,7) == "http://", substr(x,8,length(x)),x)
So using apply to go by row and substr to find if the first 7 characters are "http://". If they are replace without the http and if they're not then replace with just x.

Between in a CFIF statement

I am sending a comma delimited list from a formset and each of the possible selected items except the first can only be added if the first is not selected.
QUANTY=10-1,1-0,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0
Each element in the list is a number, a hyphen and another number. The first number is the product number and the second is quantity.
If the first element in the list is 10-1 you cannot add any other items so they would have to be number -0 as in 3-0.
This statement works if any of the other elements contain -1
<cfif ListFirst(QUANTY) IS "10-1" AND ListRest(QUANTY) contains "-1">
<tr>
<td align="center">
<font color="#FF0000"><strong>
You cannot add any features to the Basic Plan<p>
Please click "Back" and reset your order.
</strong></font>
</td>
</tr>
<cfabort>
</cfif>
The problem arises when a quantity larger than 1 is selected as in:
QUANTY=10-1,1-0,2-0,3-0,4-0,5-0,7-0,6-0,8-4,9-0,15-0,13-0
I cannot seem to get the "Between" function to work in the part of my cfif statement:
AND ListRest(QUANTY) contains "-1">
as in:
AND ListRest(QUANTY) BETWEEN "-1" and "-50">
Any suggestions appreciated
ListRest(quanty) contains "1"
will return true on values such as "1-0". That's clearly not what you want.
I suggest treating your string as nested lists. The outer list is comma delimeted and the inner is hyphen delimited. This approach should work
<cfif ListLast(ListFirst, Quanty, ","), "-") gt "0">
loop through the rest of Quanty and use ListLast to check
the values after the hyphen
<cfelse>
code for this condition
</cfif>
I tried the script above and got an error on the ,...
I wrote this and it seems to work ok:
<cfset qty1 = "#ListFirst(QUANTY)#">
<cfset qty2 = ListLast(qty1, "-")>
<cfif (qty2 GT 0) AND
((ListRest(QUANTY) contains "-1") OR
(ListRest(QUANTY) contains "-2") OR
(ListRest(QUANTY) contains "-3") OR
(ListRest(QUANTY) contains "-4") OR
(ListRest(QUANTY) contains "-5") OR
(ListRest(QUANTY) contains "-6") OR
(ListRest(QUANTY) contains "-7") OR
(ListRest(QUANTY) contains "-8") OR
(ListRest(QUANTY) contains "-9"))>
do this
</cfif>
Thanks and if there is a shorter way to write it I would appreciate the input.
Your solution should work, but a Regular Expression might be more elegant.
<cfif ListFirst(QUANTY) is "10-1" and REFind("-[1-9]",ListRest(QUANTY))>
Do something
</cfif>
Should work. Reg Expressions are really helpful. If you haven't used them before, the one above looks for a dash followed by a number from 1 to 9.
I would suggest just using a regex by itself (no need to do list manipulation to check if a pattern exists):
<cfif REFind("^[0-9]+-[1-9].+-[1-9]", QUANTITY)>
<tr>
<td align="center">
<font color="#FF0000"><strong>
You cannot add any features to the Basic Plan<p>
Please click "Back" and reset your order.
</strong></font>
</td>
</tr>
<cfabort>
</cfif>
Tested on the following:
"10-1,1-0,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0" no match (pass)
"10-0,1-5,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0" no match
"9-1,1-5,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0" match (fail)
"10-1,1-5,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0" match
"11-1,1-5,2-0,3-0,4-0,5-0,7-0,6-0,8-0,9-0,15-0,13-0" match
"10-1,1-0,2-0,3-0,4-0,5-0,7-5,6-0,8-0,9-0,15-0,13-0" match