Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion breaking an array into two

Can I split an array into two separated ones with each element in the original array separated by a ":"? The text before the ":" goes to array1, the text after ":" goes to array2

<cfset tempArr = DeserializeJSON(URL.data) />
<cfset selectList = "" />
    <cfloop array=#tempArr# index="i">
<cfset selectList = listappend(selectList,i) />
</cfloop>

Right now this code grabs the whole element and not separately.

Edit

A sample string would be:

first_name:Bob


first_name goes into selectList1 Bob goes into selectList2


The grand scheme of things would have other fields likewise:

first_name:Bob

last_name:Shmo

age:27

etc...

EDIT: Answer

Problem was solved by using the code

<!---Variables--->
<cfset temp1 = "" />
<cfset temp2 = "" />
<cfset selectList1 = "" /><!---Holds column names for tables--->
<cfset selectList2 = "" /><!---Holds query parameters for column names. Ie,
                                    values for use in the WHERE clause--->

<cfloop array=#tempArr# index="i"><!---Loop through contents of the array--->
    <cfset temp1 = GetToken(i,1,":")/><!---Store the column name--->
    <cfset temp2 = GetToken(i,2,":")/><!---Query parameter--->

    <cfset selectList1 = listAppend(selectList1, temp1)/><!---Adds to list of column names--->
    <cfset selectList2 = listAppend(selectList2, temp2)/><!---Adds to the list of query parameters--->
</cfloop>
like image 712
reZach Avatar asked Jun 05 '13 19:06

reZach


2 Answers

I think without seeing your array example, you mean splitting the data in the array into two lists?

<cfset selectList1 = listAppend(selectList1, listFirst(i,':')) >
<cfset selectList2 = listAppend(selectList2, listLast(i,':')) >
like image 120
williambq Avatar answered Nov 01 '22 20:11

williambq


<cfscript>   
variables.lstString             = "First_Name:John,Last_Name:McClane";
variables.lstFields             = "";
variables.lstValues             = "";

for(variables.i=1;variables.i lte listlen(variables.lstString,',');variables.i++){
    variables.lstFields         &= (listlen(variables.lstFields) gt 0) ? ",#getToken(getToken(variables.lstString,variables.i,','),1,':')#" : getToken(getToken(variables.lstString,variables.i,','),1,':');
    variables.lstValues         &= (listlen(variables.lstValues) gt 0) ? ",#getToken(getToken(variables.lstString,variables.i,','),2,':')#" : getToken(getToken(variables.lstString,variables.i,','),2,':');
}

writeDump(variables.lstFields);
writeDump(variables.lstValues);
</cfscript>
like image 31
Jarede Avatar answered Nov 01 '22 20:11

Jarede