From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Leo Butler Newsgroups: gmane.emacs.help Subject: Re: count char in string [The Emacs Challenge Competition - round 2, with fallout] Date: Thu, 29 Oct 2020 12:52:14 -0500 Message-ID: <86sg9wdinl.fsf@x201.butler.org> References: <87r1pj41wa.fsf@zoho.eu> <875z6tqh10.fsf@pmatoe> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25213"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Oct 29 19:08:14 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kYCLN-0006Oe-Gl for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 29 Oct 2020 19:08:13 +0100 Original-Received: from localhost ([::1]:45640 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kYCLM-0002VU-IR for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 29 Oct 2020 14:08:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kYCKb-0002U9-VT for help-gnu-emacs@gnu.org; Thu, 29 Oct 2020 14:07:26 -0400 Original-Received: from mail-eopbgr670111.outbound.protection.outlook.com ([40.107.67.111]:62610 helo=CAN01-TO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kYCKZ-0001zE-NR for help-gnu-emacs@gnu.org; Thu, 29 Oct 2020 14:07:25 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aBIylI9vEfdmo29SW5akfyBBJeUWKzB6oLt5bw32EDpXiHuKzJ1HNS2tQqzboGArkLPXcAs/pYoROATZkAyX+85RMWg5utlnCIHaOcZkNfQV+E0TRBrIZ7DOqSnsuuAlnPf80r7z98Ng2jgeA4qDIGGwbapMqOkEHzXzrjPLSvPpIddYE5QFY0/vZZZJbPQGNQrYX0Vts44l2gH//52sP2nYtz9dc/xlFP3GM9giComIiQ/TjrylFCexIP106YVNKNCOSk5jfKUTN9708hfcnXIACGl8cqHMGqunbTxPvAeLPSiHdcm6NKMuAmTl5vDKRIlaZxhzV8mDz38HeKgnbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2b29G+o+rg8HGSKCIlON7le3auoAxJYulxVgCQg00+g=; b=eh2e63y9TkgG41FdmOrWc0ugkPblTVZtPzyzxrycmsWQehNNrKF6Si1usMSP1Aavn3+I/K0K8HMw9+Ng+Dlz99mE8ZK0R8tlEeAI2R1MhQSlXDTzdsEvzjlmuuC37XTTuKgfyK3cvWTXdz6VDXbNK6mVvb183YtMYiplc7W+6TuqRnMhtJPbd4lcRKHHgx7fa/MHSrnKtb4vmvuj0XORJuPLDjgW1YzLNy+1IHTZ1Cb0D7bN38TzGYa7Afcp1GjPq9jUqxzl91XThj8y7HfpBMc7aIeQ01K6bBU7WEufc4V5GhUlq0C8JHMFMXlYiqFVffnLZ9/5I8K7aYWB6QkWUw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=umanitoba.ca; dmarc=pass action=none header.from=umanitoba.ca; dkim=pass header.d=umanitoba.ca; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umanitoba.onmicrosoft.com; s=selector2-umanitoba-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2b29G+o+rg8HGSKCIlON7le3auoAxJYulxVgCQg00+g=; b=AIudyzKZS72/aOq4E/Cbr3hnhIbHEgRdSXmn4ZWuf61FsVynPHLT+WSlYVLkmropDLIloXcAoE2Y2Q2S2Imo+BdwrNgjKfyxvZB+LpgodVDlUV4qw9kk/hZ2XYEkWPqbXsnZOLD/pC0rD6TKRIgcLh+AYjLl8awJklZiuwEoyJk= Authentication-Results: gnu.org; dkim=none (message not signed) header.d=none;gnu.org; dmarc=none action=none header.from=umanitoba.ca; Original-Received: from YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:21::31) by YTOPR0101MB1564.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00:17::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.25; Thu, 29 Oct 2020 17:52:18 +0000 Original-Received: from YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM ([fe80::d9ac:774a:ace:9c4e]) by YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM ([fe80::d9ac:774a:ace:9c4e%3]) with mapi id 15.20.3499.027; Thu, 29 Oct 2020 17:52:18 +0000 In-Reply-To: <875z6tqh10.fsf@pmatoe> (Patrick McAllister's message of "Thu, 29 Oct 2020 14:53:12 +0100") X-Originating-IP: [45.44.33.194] X-ClientProxiedBy: MWHPR04CA0067.namprd04.prod.outlook.com (2603:10b6:300:6c::29) To YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:21::31) X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from x201 (45.44.33.194) by MWHPR04CA0067.namprd04.prod.outlook.com (2603:10b6:300:6c::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18 via Frontend Transport; Thu, 29 Oct 2020 17:52:17 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9d91f10f-6ef1-47c6-4c1e-08d87c3360b7 X-MS-TrafficTypeDiagnostic: YTOPR0101MB1564: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HnFMW2uRsjGnvqcZJPlf0i/uFhEXhDm5QIROZyfXsZbXqx2rcBrXkj2iWiwny3Rfk2gWcaiN9C+kYnEhiOJwiZgatUC42SU9U+aZg1mxdmUuSB/oQU2sP8IpRRIAVyofdTcdxwfoKjkTSbShP0/WwjoIcc1Q7+MShUwUrdmxwZfFS2fCJA+xn/6pM8waAU6mFiyUjbpYzlZnA3w1/26/vgKyzF6zDAblOS+XEyB0YIL/zSYkPX8wn5vNrMy+xMnm4EQDYt0wKkBPFU5oFP/vQstNPZnz3PLAE3LlIrE3a+7QzF0a68Eya1Xm+wU4EI9x X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(346002)(396003)(376002)(136003)(39860400002)(366004)(6486002)(2906002)(478600001)(8676002)(26005)(5660300002)(956004)(83380400001)(8936002)(6496006)(52116002)(44832011)(16526019)(66476007)(66556008)(66946007)(6916009)(6666004)(786003)(186003)(316002)(9686003)(86362001); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: Gt/joobgmlggGz/EumA8JJHqFRqoQbQGuG3GF5nwg3qaiWlFPQX3WSxjljBQ0jd3UuCz6c5gocPkawBTW7bAocIU5wcWcKYM/pQp57fq0UTwX/X39pDRekPS4l2kWRsA+Q97T0hnJ8k3q3hhDcpiFpDBuWpBNraFvtpsU2srZ57LeHW10IM3504I8y2Aa6x7psla0ikuHbVdmCED7ilq3nCM0qeMf0HxC2/xiQCuHT2lIEkDrXBtWZp/oFOgvN+VtFUith6utJxPvB1BjooU5pQX+Q26nXun8BBagyTun3e1rjXtSOz+YVC061ecN9aRwVVXmL222o8KzxL6MZPPqhTVQKpU9aN1xb30ncaOBXersQqXBH91CA6dpJxnb9GtLgD10+x0KC1i0H+K7T+JiAedYmel8K8uqNCBNSymktlFtNx99myGMMS61h2+F+bsFAQgZ+d43+bnOQR7UPfvOqSiw5sGpKb9Ah23ZOE8ynBOuJYLxjijJjVE2YCAJa4uE0fu/XKiOJPeMVTcFD89j6QXivhLIeVEjl4SQgjm/XWHIL3z4dMI89RK8msl49NNAZvB3E6rZQwoaZXICe5ejTUlBkc8Bjj2GLPyhYuGPCKuCUaUr0u+YuRbYqNUjlUEiCMTO/aG5Hvzrnu0aQ7HIA== X-OriginatorOrg: umanitoba.ca X-MS-Exchange-CrossTenant-Network-Message-Id: 9d91f10f-6ef1-47c6-4c1e-08d87c3360b7 X-MS-Exchange-CrossTenant-AuthSource: YTBPR01MB3053.CANPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2020 17:52:18.7323 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4f80dd0b-338c-4e4c-8a14-90446962f7b8 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: SarI150GO1EGDVbkKd5ohWiZKFbb5e/EQub3sWFciV242xH6oJKmA/kGYqFeT/gGEEE/dmJkeWbwq+IY0kTqlQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: YTOPR0101MB1564 Received-SPF: pass client-ip=40.107.67.111; envelope-from=Leo.Butler@umanitoba.ca; helo=CAN01-TO1-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/29 14:07:21 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:124797 Archived-At: Patrick McAllister writes: > ******************************************************** > Caution: This message was sent from outside the University of Manitoba. > ******************************************************** > > On Wed, Oct 28 2020, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > >> round 2: >> >> Very simple: we are looking for a function that returns the number of >> occurrences of a char in a string. >> >> Emanuel Berg's solution: >> >> (require 'cl-lib) >> (defun count-char-in-string (the-char str) >> (let ((c (if (characterp the-char) the-char (string-to-char the-char))) >> (cs (string-to-list str)) ) >> (cl-count c cs) )) >> ;; (count-char-in-string ?a "Emacs skills kills") ; 1 Here is a solution for interactive use that is far more flexible than just counting characters. I use the following a lot when editing documents with a maximum character count: M-< M-x replace-regexp [^[:space:]] \& RET Or, since I bind replace-regexp to C-x a r, M-< C-x a r [^[:space:]] \& RET I just did that and the mini-buffer informed me there were 1232 non-space characters in the buffer. Leo