From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Mon, 12 Oct 2015 21:43:28 +0000 Message-ID: References: <874mhvq3b2.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1444686240 12509 80.91.229.3 (12 Oct 2015 21:44:00 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 12 Oct 2015 21:44:00 +0000 (UTC) To: Marcin Borkowski , Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Oct 12 23:43:59 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zlksr-0004GW-HX for geh-help-gnu-emacs@m.gmane.org; Mon, 12 Oct 2015 23:43:53 +0200 Original-Received: from localhost ([::1]:58953 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlksr-0004az-6m for geh-help-gnu-emacs@m.gmane.org; Mon, 12 Oct 2015 17:43:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55030) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlkse-0004at-OF for help-gnu-emacs@gnu.org; Mon, 12 Oct 2015 17:43:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zlksd-0007q0-Dq for help-gnu-emacs@gnu.org; Mon, 12 Oct 2015 17:43:40 -0400 Original-Received: from mail-ob0-x233.google.com ([2607:f8b0:4003:c01::233]:34238) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlksd-0007pv-8N for help-gnu-emacs@gnu.org; Mon, 12 Oct 2015 17:43:39 -0400 Original-Received: by obbda8 with SMTP id da8so116190573obb.1 for ; Mon, 12 Oct 2015 14:43:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=xLideLoZ3Ow5KoGS5OBGvATPDbvgxE/fZuwap66Nrts=; b=jLHyTjHFe20pe0KxvIxY6Pk9ae+iGsYiYUtKpKRhzelv5SbnVwb6Cd6541r+dmBX2A zZTtyJxBuMI2Vx2Y6hn/WokFnh/ftDzrtXuEHyJDn5DqLoh3zB7Ap11JTx0tqZZOei57 TVoeyrK0ay4TH9/NpG6UYNoWycTfUR/JVJoHzGUuL/2SdDi4ldm1Uuflcoa6GoWyGo9R mjyK4PU6IPOpaIL5mjWSQymhepqLbl1J/1nkAX5X06TB0rNhuMiK+5aB2x0ma1V0If00 b2D0xXYipZ3aCirS8zpTLrv9sYkQcFc4HJCz9dZAxIO+D8OVAeQcI0LzaFZRRh7wSQz/ M4CQ== X-Received: by 10.182.126.169 with SMTP id mz9mr4907328obb.63.1444686218494; Mon, 12 Oct 2015 14:43:38 -0700 (PDT) In-Reply-To: <874mhvq3b2.fsf@mbork.pl> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::233 X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107597 Archived-At: I took it up as an elisp exercise and got the below (not sure if it is performance optimal for your application): The catch form returns the number of matches. (let ((char ?a) (str "abcda")) (catch 'break (let* ((str str) (len (length str)) (num-matches 0) match-pos) (dotimes (i len) (setq match-pos (string-match-p (char-to-string char) str)) (when match-pos (setq str (substring-no-properties str (1+ match-pos))) (setq len (length str)) (setq num-matches (1+ num-matches))) (when (= 0 len) (throw 'break num-matches)))))) Below is the same code as above with some debug statements: (let ((char ?a) (str "abcda")) (message "%0d occurrences of `%c' char found in \"%s\"" (catch 'break (let* ((str str) (len (length str)) (num-matches 0) match-pos) (dotimes (i len) (message "i = %0d str = %0s" i str) (setq match-pos (string-match-p (char-to-string char) str)) (when match-pos (message "match-pos = %0d str = %0s" match-pos str) (setq str (substring-no-properties str (1+ match-pos))) (message "new str = %0s" str) (setq len (length str)) (setq num-matches (1+ num-matches))) (when (= 0 len) (throw 'break num-matches))))) char str)) On Mon, Oct 12, 2015 at 3:45 PM Marcin Borkowski wrote: > Hi all, > > the subject line says it all. Is there any built-in Elisp function to > do that? Should I just use cl-count? > > Best, > > -- > Marcin Borkowski > http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski > Faculty of Mathematics and Computer Science > Adam Mickiewicz University > >