0

Given the array below, how do I convert it to a simple list of values?

<cfdump var="#arguments.ServiceTextArray#">

Array Elements:

   1 3567_no 
   2 3584_yes 
   3 3642_yes 
   4 3643_yes 
   5 3644_no 
   6 3645_no 
   7 3646_no 

Specifically, how do I extract values with the "yes" suffix and produce a list like this?

3584,3642,3643

Thanks in advance.

Leigh
  • 28,765
  • 10
  • 55
  • 103
TGR
  • 107
  • 1
  • 3
  • 11

4 Answers4

2
var list = "";

for (item in array)
    if (ListLast(item, "_"))
        list = listAppend(list, val(item));
Henry
  • 32,689
  • 19
  • 120
  • 221
  • 1
    I think they only want "yes" values. – Leigh Jun 18 '12 at 20:40
  • @Leigh `yes` is a boolean value in CF, so what `listRest(item, "_")` returns will do. – Henry Jun 19 '12 at 05:07
  • 1
    @Henry lol, clever, but I think I'd be a bit frustrated if I came across that bit of code while doing maintenance. – Jake Feasel Jun 19 '12 at 05:10
  • 1
    @JakeFeasel really? Well, it's like doing `if (x == true)`, kind of unnecessary. However, if the listRest can be some value other than `yes` or `no`, then == "yes" would be a better choice. – Henry Jun 19 '12 at 05:14
  • 1
    It is a bit like that, but you have to admit it a bit unusual to expect listRest to just pop out a boolean like that (vs. the not so unusual experience of a simple variable being evaluated as a boolean). – Jake Feasel Jun 19 '12 at 05:18
  • @JakeFeasel I found many script-based language users use boolean comparison very loosely, to keep things loose. So I took that into my style. :) – Henry Jun 19 '12 at 05:22
  • @Henry - Neat trick. Though I am with Jake on this one. Given that it is not a simple variable, my preference is usually clarity over clever ;-) But it is a matter of personal style. – Leigh Jun 19 '12 at 12:14
  • 1
    There's a big clue that a boolean is coming: *the line starts with **IF*** – Peter Boughton Jun 19 '12 at 22:50
  • 1
    (Though I would do `ListLast` instead of `ListRest`, because that's less brittle.) – Peter Boughton Jun 19 '12 at 22:52
  • Sure you could figure it out, just not as intuitively. Unlike the alternatives, it is less clear what constitutes valid input within the current context: 507, 1, -4, yes or any of the above? In that regard the other approaches are more intuitive. But like I said, a lot comes down to personal preference. – Leigh Jun 21 '12 at 19:54
2

Would this help? There will always be better solution than this.

<cfset myList = "">
<cfloop from="1" to="#Arraylen(myArray)#" index="index">

    <Cfif right(myArray[index],3) EQ "yes">
        <cfset myList = listAppend(myList, listFirst(myArray[index], '_'))>
    </Cfif>
</cfloop>

<cfoutput>#myList#</cfoutput>
DG3
  • 5,070
  • 17
  • 49
  • 61
  • 1
    Do not forget to separate the list elements with commas. Also, `listAppend` is simpler than string concatenation ie `&` – Leigh Jun 18 '12 at 20:41
  • 1
    FWIW: if you want only the number portion, just split the value inside the loop: ie `myList = listAppend(myList, listFirst(myArray[index], '_'))` – Leigh Jun 18 '12 at 20:47
  • I got the results like this: 3584_yes 3642_yes 3643_yes but how to convert its list only numbers..(3584,3642,3643) – TGR Jun 18 '12 at 20:50
  • Good. Do not forget to mark DG3's response as the answer if it solved your problem. – Leigh Jun 18 '12 at 21:23
2

This is partly an extension to Henry answer but should be exactly what your after:

<cfscript>
    tmpArray = ['567_no','584_yes','3642_yes','3643_yes','3644_no','3645_no','3646_no'];
    list = "";
    for (item in tmpArray)
        if (listLast(item, "_") == "yes")
            list = listAppend(list, listFirst(item, "_"));

writeDump(list);

Paul
  • 1,577
  • 7
  • 10
1

Using the Underscore.cfc library (CF 10 only):

filteredArray = _.filter(arguments.ServiceTextArray, function(val) {
    return (val contains 'yes');
});

resultArray = _.map(filteredArray, function(val) {
    return left(val, 4);
});

list = arrayToList(resultArray);

(I created this library, BTW)

Russ
  • 1,931
  • 1
  • 14
  • 15
  • Nice one. I did not realize you could do that with CF10. Btw, is that your library? If so, you may want to add the standard affiliation disclaimer to responses [per SO rules](http://stackoverflow.com/faq#promotion). – Leigh Jul 01 '12 at 20:15
  • Thanks for the info, I've added the disclaimer. Hope you find the library to be useful! – Russ Jul 02 '12 at 16:08