-1

I have this function :

function MaFonction10()
     let myList1 = ["self-made-man", "rendez-vous", "forget-me-not"]
     for elem in myList1
        echo len(elem)
     endfor
endfunction

I vould like to get the number of hyphens or letter "a"s (for example) in each item, instead of the total number of letters.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65

2 Answers2

0

You can use split() on a string, with a pattern of \zs, to split it into a list of characters.

Then you can use filter() on that list to only keep the matching character.

Finally, you can take the len() of the resulting list.

For example, to count the number of dashes in "elem":

echo len(filter(split(elem, '\zs'), {_, val -> val ==# "-"}))

UPDATE:

As suggested by @D.BenKnoble, using the more modern method syntax makes this nicer:

echo elem->split('\zs')->filter({_, val -> val ==# "-"})->len()

As noticed by @ArseneAsedrith, we don't need to go through all this trouble with splitting and filtering, since the count() function takes care of this exact case:

echo count(elem, "-")

Or:

echo elem->count("-")
filbranden
  • 28,785
  • 3
  • 26
  • 71
-1

Thank you. I have an other solution too :

function! MyFunction()
let myPonct = ["-"]
let myList = ["self-made-man", "rendez-vous", "forget-me-not"]
For elem in myPonct
        for elem2 in myList
            echo count(elem2,elem) 
        endfor
Endfor
endfunction
  • Good job of finding count()! But unfortunately your answer doesn't highlight that and just throws that into a code block that has syntax errors (For, Endfor), has an unnecessary loop around the separator (why not just let separator = "-" instead?), particularly using not very descriptive names, such as elem2 and elem for the items... You could improve this answer by highlighting the count() function, explaining how it works and only showing a short snippet displaying it rather than a whole function. – filbranden Mar 28 '20 at 17:28
  • 2
    Also: I don't really understand why you keep posting self-answers to your questions... While self answering is typically acceptable, the way you have been using it doesn't look reasonable at all. Are you asking these questions in good faith of asking for help in solving an issue you're having? Furthermore, you're not upvoting and accepting answers when others post them to your questions. That's not helping you either, since that's not encouraging users to answer your questions. – filbranden Mar 28 '20 at 17:34
  • Totally agree with the preceding comment. We (DBK and myself) very politely and patiently pointed out similar concerns a week ago. Not only were the comments disregarded but the same behavior continues unabated. At some point all the experts here are going to just ignore your questions. I notice you have a number of downvotes on your questions. I wouldn't be surprised if that's other people starting to send signals of their own. No one likes being taken advantage of Arsene. What's your problem that you can't be respectful, follow the simple guidelines, etc? – B Layer Mar 31 '20 at 10:03