Created
December 29, 2021 16:23
-
-
Save chilkari/f356ceec251897fb28261f84e6af9acb to your computer and use it in GitHub Desktop.
TDM CSV parsing in cold fusion - https://cffiddle.org/ for testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfhttp url="https://www.adobe.com" | |
method="get" result="httpResp" timeout="120"> | |
<cfhttpparam type="header" name="Content-Type" value="application/json" /> | |
</cfhttp> | |
<cfsavecontent variable="csvData"> | |
1,tdm | |
2,classic | |
3,tdm | |
4, classic | |
5 , tdm | |
6 , classic | |
"7","classic" | |
"8" , "tdm" | |
9, "classic" | |
Include some bigger numbers to ensure matching works > 1 digit | |
(Note that any text like this that doesn't match is ignored | |
and doesn't break anything...) | |
10, classic | |
100, tdm | |
1000, classic | |
10000, tdm | |
Back down to some smaller numbers, but no line breaks: | |
11,tdm 12,"classic" "13","tdm" | |
Let's inlcude one last row after some more bogus, non-matching text | |
to ensure that this last item: the meaning of life, is included. | |
42,classic | |
</cfsavecontent> | |
<cfscript> | |
csvRegExp = '\s*"*(\d+)"*\s*,\s*"*(classic|tdm)"*\s*' | |
// Aligned Arrays for Company ID and Type | |
companyIDs = [] | |
conversionTypes = [] | |
matches = REMatch(csvRegExp, csvData); | |
for (i=1; i<=arrayLen(matches); i++) { | |
match = matches[i] | |
result = REFind(csvRegExp, match, 1, true) | |
// Note: No guards needed here because the initial REMatch will only | |
// pass in lines which match and have the expected components. | |
// [2] and [3] are guaranteed to be there with the right pieces. | |
arrayAppend(companyIDs, result.match[2]) | |
arrayAppend(conversionTypes, result.match[3]) | |
} | |
writeDump("IDs") | |
writeDump(companyIDs) | |
writeDump("Types") | |
writeDump(conversionTypes) | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment