使用List从中间,第一个位置和最后一个位置依次删除元素

时间:2018-06-11 20:01:16

标签: coldfusion

尝试处理列表删除但仍停留在ts删除过程中:这是我正在处理的小小点,任何帮助表示赞赏

  

https://trycf.com/gist/aa3871e76db36ad446b770b9bbbb4cec/lucee5?theme=monokai

<cfset lstFirst = "1,2,3,4,5">

<cfset lstMiddle = "6,7,8,9,10">

<cfset lstLast = "11,12,13,14,15">

从第一个开始,我应该可以像5,4,3,2,1一样从反向删除,但我不应该 从下一个2

项目的中间删除

如果中间的那个是8,那么从lstMiddle开始我应该能够删除7,6等等或9,10但是我不应该 删除6,10而不删除79

从lstLast完全反转lstFirst,其中选择了最后一个元素,我应该能够删除 从14,13,12开始,但无法从中间或从11开始删除,它必须从1415开始向后删除这是我尝试使其工作的方式

1 个答案:

答案 0 :(得分:0)

**<cfoutput>
    <cfset lstFirst = "1,2,3,4,5">
    <cfset lstMiddle = "6,7,8,9,10">
    <cfset lstLast = "11,12,13,14,15">

    <!--- It's work all dynamic values for lstFirst like 1,2,3,4,5 OR 1,2,3,4,5,6,7,8,9,10 OR 1,2,3,4,5,6,7,8,9,10,11,12 etc.... --->
    ======= Delete all number from reverse and except very next item to middle ====

    <cfset convertToArray = listtoarray(lstFirst)>
    <cfset getRoundValue =round(arraylen(convertToArray)/2)>
    <cfset findMidNextEle = ArraySlice(convertToArray,1,getRoundValue)>

    <cfset LfirstMiddle = listgetat(lstFirst,listfind(lstFirst,listlast(arraytolist(findMidNextEle)))-1)>

    <cfloop from="#listlen(lstFirst)#" to="1" index="i" step="-1">
        <cfset listgetVal = listgetat(lstFirst,i)>
        <cfif LfirstMiddle NEQ listgetVal >
            <cfset lstFirst =listdeleteat(lstFirst,i)>
        </cfif>
        <br/><cfdump var="#lstFirst#" /><br/>
    </cfloop>



    =========== Delete 7,6 and so on or 9,10 and  delete 6,10 without deleting 7 and 9 ================================= <br/>

    <cfset lstMiddleMiddle =  listfind(lstMiddle,listgetat(lstMiddle,listfind(lstMiddle,listlast(mid(lstMiddle,1,listlen(lstMiddle)))))) >

    <cfloop from="1" to="#listlen(lstMiddle)#" index="j">

        <cfset userList = listlen(lstMiddle)>

        <cfif userList GT 3 >
            <cfset lstMiddle1 = listdeleteat(lstMiddle,lstMiddleMiddle-1)>
            <cfset lstMiddle2 = listdeleteat(lstMiddle1,lstMiddleMiddle)>

            <cfset lstMiddleMiddle = listfind(lstMiddle2,listgetat(lstMiddle2,listfind(lstMiddle2,listlast(mid(lstMiddle2,1,listlen(lstMiddle2))))))>
            <cfset lstMiddle = lstMiddle2>
            <br/><cfdump var="#lstMiddle2#" /></br>
        <cfelse>
            <cfset lstMiddle3 = listdeleteat(lstMiddle2,1)>
            <cfset lstMiddle4 = listdeleteat(lstMiddle3,2)>
            <cfbreak/>
        </cfif>
    </cfloop>
    enter code here
    <cfdump var="#lstMiddle4#" /> <br/>
    =============== Delete from reverse except last number ==========

    <!--- It's will work for all dynamic values --->

    <cfset LastEle = listlast(lstLast)>
    <cfloop from="#listlen(lstLast)#" to="1" step="-1" index="k">
        <cfset getCurrentValue = listgetat(lstLast,k)>

        <cfif LastEle NEQ getCurrentValue>
            <cfset lstLast =listdeleteat(lstLast,k)>
        </cfif>
        </br><cfdump var="#lstLast#" /><br/>
    </cfloop>
</cfoutput>**