From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.help Subject: Re: [External] : Find the longest word in the word list file. Date: Wed, 11 Aug 2021 11:52:58 +0200 Message-ID: References: <20210811064643.GB21436@tuxteam.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28147"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (windows-nt) Cc: help-gnu-emacs@gnu.org To: Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Aug 11 11:58:50 2021 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 1mDl0b-0006zn-Fs for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 11 Aug 2021 11:58:50 +0200 Original-Received: from localhost ([::1]:45012 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mDl0a-0007Uo-G2 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 11 Aug 2021 05:58:48 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52732) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mDkzv-0007Oj-TZ for help-gnu-emacs@gnu.org; Wed, 11 Aug 2021 05:58:08 -0400 Original-Received: from mail-am7eur06olkn20807.outbound.protection.outlook.com ([2a01:111:f400:fe10::807]:51424 helo=EUR06-AM7-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 1mDkzt-0002SJ-Qc for help-gnu-emacs@gnu.org; Wed, 11 Aug 2021 05:58:07 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BNlOLQGgb1IhxqW2rs2Q+z3Tl4zGBp8QkzApbG6tbT6FY7okz2XSqTQd4qaNANvh8CH6kW/PfMbLkMfUCop1IwQPxBsDZiniILxipi9qhqaYdgziDxCSXadVZEem5hS2wb1Fjat9yjQftahl+mTWplyioC+8YMtWWwQcovP3lZe7OMQwrQ2u5jjGXMiDqbpQNCP9DrsDrXi9D8whPi30J67vN9BKO2hHsUGcedNiPmnP6w0DwwYti3u9hkUDnvLIHQ8GlZGysxmvCJfSmg/3uOW/Ny3gOOQyKu7eQ21T9Za4gKpzoWGQoZGRITzJdHslUWeZkFz4jWU87SDG5dO6qA== 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=ATW3i6lI04nA9vNtYrnCJ74QHjhQO2mPltQbIaS5IRs=; b=dqaQZZMkH9UhhoZcXT0nh5kHMIIhilcmKx9IZWT1IyUyes8MQJkcaGyV6GICkFynJPWKGwUl9eA1YLPLBHsSqEZ/nTn6quAFWO3iewemlD/DMXhnY0zPSsaZuJJEyyav4mYnnEq7AiPWhcGoYeYenOe0NGiS8X7jikRs+GDt6kyQJ46sLYGUMjhgPOqFJuaaNAFIjnYLVCqF98kfjCenttt8ZwkRU1kyclV+IXVIc1ZSVGn2QzYYsce9xQUJCwS55uVdqsElaAgX89YGZjY4jQEp21JOvvLgqjswUu7msz+8LmacEjhnpXeo0btr/ccrANYDWwxTf37IcSW/0Fxc4Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ATW3i6lI04nA9vNtYrnCJ74QHjhQO2mPltQbIaS5IRs=; b=Q9rYE4JHpYbQxNQC//n437mB61VAcnrECrq++HqOvQVlEcKwd+8t9CVnn79YrRlw9iQSRm2zhRcPlF19gVdM/j8y5Z+jWNScpw9qlxHK87iadqkxRU8+ssAPfzvmmN7zZWiVfrQT/Drr6kQyXqSap4ga10/D4JQFtjibLRSu08SdBFp9cEXBF++TyORI5nEmg2BfUcM/ZYsKRS1eG2dQjAiOUdLOlBi5DUIiIkHj0xxq5fIWujybSI/IHU6gPrjx1JN9O6iGULPqV54eVLmPYp2Tgfs3rF5B2BeNLPs5ILPRjF1nXX3Ll8PONOc/zf1gpzjGmOoJy0xRbpUAxN/v6g== Original-Received: from AM7EUR06FT057.eop-eur06.prod.protection.outlook.com (2a01:111:e400:fc36::4a) by AM7EUR06HT042.eop-eur06.prod.protection.outlook.com (2a01:111:e400:fc36::96) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.14; Wed, 11 Aug 2021 09:53:01 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2a01:111:e400:fc36::4e) by AM7EUR06FT057.mail.protection.outlook.com (2a01:111:e400:fc36::445) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.16 via Frontend Transport; Wed, 11 Aug 2021 09:53:01 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:7F92A7B1F70C7A62BBB32AC6E19DEE700C3B998B4C003F566F2F7091248CC977; UpperCasedChecksum:2CA5F93D2398BB8CFD6B331B2EF307366065E2A447A267414AA924A425A6DAA1; SizeAsReceived:7656; Count:46 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::6558:f201:6d1a:3f39]) by AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::6558:f201:6d1a:3f39%2]) with mapi id 15.20.4415.015; Wed, 11 Aug 2021 09:53:01 +0000 In-Reply-To: <20210811064643.GB21436@tuxteam.de> (tomas@tuxteam.de's message of "Wed, 11 Aug 2021 08:46:43 +0200") X-TMN: [lcq9OTnyJYRGB0oSEEMA6Mg2QjPhYJ4k] X-ClientProxiedBy: AS8PR04CA0156.eurprd04.prod.outlook.com (2603:10a6:20b:331::11) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <827dgs8g05.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from HP-Laptop.homepc (81.232.177.30) by AS8PR04CA0156.eurprd04.prod.outlook.com (2603:10a6:20b:331::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.14 via Frontend Transport; Wed, 11 Aug 2021 09:53:00 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: db464dca-b8dc-4d2e-ee53-08d95cadcdf4 X-MS-TrafficTypeDiagnostic: AM7EUR06HT042: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wWv5CjCDxIBLRfCI49UNgJNQa5YcXPdDgTD3W7GTPxZflw7z/VeOXFacH5JUOK2DW+2iKr8wXhrFoCKmrsyIwfN94lRnT2DL6m9UqHym2L/Pipnx6irYch88ArxI+NuTOo9Kfy7YjLe2Jgzl9JghTzG4jkkT9glcjVgHodPwW4VYyfMg1Cw2wWT0D74sdtACfuM/DEdzb4+++RvhS1VP/JStdMjGvbxMb00X0WonZ+wEGKyuIb5smxHfQbR6FyddATc0EkzjNkEQGe2DiO4xTKoQVxPXD357iTIaVxDCevnIcy+aCvqp8IoE9NJ1UjiE2/kJxbx+vvVXvQ8ex3cvOA15UODY13yfBFuWWqR2R3+UkIktXvUwlEBAJR/Ozs6GDf40Cdd7b1N8mQ+ItPTXICPKs7pbafBwm20NR+rfz36/mUjKl1pHy7zpnP0MLG1Z X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TY3eqm5E1KTiA7j6nSGjipEYxbEUId4F7C2bDP0nm+oHjYsXi8e+e64knEszmYPmd9IOJzAXOlSz6RBF5dUB4RObJSfn133flfapXqlTB2y1WUIzwT2JxbJd2QnXbwiuxJ2uhr8/r7X1WkrbtckFUA== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: db464dca-b8dc-4d2e-ee53-08d95cadcdf4 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Aug 2021 09:53:00.9729 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: AM7EUR06FT057.eop-eur06.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7EUR06HT042 Received-SPF: pass client-ip=2a01:111:f400:fe10::807; envelope-from=arthur.miller@live.com; helo=EUR06-AM7-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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:132448 Archived-At: --=-=-= Content-Type: text/plain writes: > On Wed, Aug 11, 2021 at 11:00:53AM +0800, Hongyi Zhao wrote: >> On Wed, Aug 11, 2021 at 9:51 AM Drew Adams wrote: > > [...] > >> Thank you for your solution, but it seems so complicated to me. > > You prefer something to hack on yourself ;-) > > OK, here's one attempt. But be careful. I just tested it > twice, so there may be two bugs left :-) > > (defun goto-longest-line () > (let ((maxpos) > (maxlen 0)) > (goto-char (point-min)) > (while > (let ((len (- (line-end-position) > (line-beginning-position)))) > (when (> len maxlen) > (setq maxlen len > maxpos (point))) > (= (forward-line) 0))) > (goto-char maxpos))) > > Cheers > - t This looks like such a typical school assignement. Usually when students have learned how to open and read files ... Here is mine solution, I had same idea as Thomas, I just did it little bit differently: #+begin_src emacs-lisp (defun longest-line-in-file (filename) (with-temp-buffer (insert-file-contents filename) (goto-char (point-min)) (let (longest (longest-length 0) (current-length 0)) (while (re-search-forward "^\\w.*\\w$" nil t) (setq current-length (- (line-end-position) (line-beginning-position))) (when (< longest-length current-length) (setq longest (cons (line-beginning-position) (line-end-position)) longest-length current-length))) (buffer-substring-no-properties (car longest) (cdr longest))))) #+end_src Run it from M-: (longest-line-in-file "path/to/some/file") or add an `interactive' statement if you prefer M-x. Also add some error checking, for valid file and valid input. It returns first occurence of the longest string in a file. For example in a test file attached: one two three four five six seven eight nine ten You will get "three" back. If you would like the last one (eight), change the checking condition. If you would like all of them, push them all into a list instead of a cons cell. You could also save length with each cons cell, as a pair of cons, or just push all three to a list, if you would like to use it for something later on. --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=test.txt one two three four five six seven eight nine ten --=-=-=--