From: Arthur Miller <arthur.miller@live.com>
To: <tomas@tuxteam.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: [External] : Find the longest word in the word list file.
Date: Wed, 11 Aug 2021 11:52:58 +0200 [thread overview]
Message-ID: <AM9PR09MB49775144840A291EB047B99696F89@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <20210811064643.GB21436@tuxteam.de> (tomas@tuxteam.de's message of "Wed, 11 Aug 2021 08:46:43 +0200")
[-- Attachment #1: Type: text/plain, Size: 2276 bytes --]
<tomas@tuxteam.de> 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 <drew.adams@oracle.com> 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.
[-- Attachment #2: test.txt --]
[-- Type: text/plain, Size: 49 bytes --]
one
two
three
four
five
six
seven
eight
nine
ten
next prev parent reply other threads:[~2021-08-11 9:52 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-10 23:14 Find the longest word in the word list file Hongyi Zhao
2021-08-11 1:51 ` [External] : " Drew Adams
2021-08-11 3:00 ` Hongyi Zhao
2021-08-11 6:46 ` tomas
2021-08-11 8:58 ` Hongyi Zhao
2021-08-11 10:03 ` tomas
2021-08-11 9:52 ` Arthur Miller [this message]
2021-08-11 10:06 ` tomas
2021-08-11 13:32 ` Hongyi Zhao
2021-08-11 13:58 ` Stephen Berman
2021-08-11 14:17 ` Hongyi Zhao
2021-08-11 14:27 ` Stephen Berman
2021-08-11 16:09 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 2:10 ` Hongyi Zhao
2021-08-12 2:29 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 3:13 ` Hongyi Zhao
2021-08-12 3:17 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 3:34 ` Hongyi Zhao
2021-08-12 3:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 3:52 ` Hongyi Zhao
2021-08-12 4:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 4:25 ` Hongyi Zhao
2021-08-12 4:38 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 4:53 ` Hongyi Zhao
2021-08-12 5:27 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 6:25 ` Hongyi Zhao
2021-08-12 6:46 ` Hongyi Zhao
2021-08-12 6:51 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 6:59 ` Hongyi Zhao
2021-08-12 2:53 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 14:47 ` Kevin Vigouroux via Users list for the GNU Emacs text editor
2021-08-12 15:45 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 15:53 ` Kevin Vigouroux via Users list for the GNU Emacs text editor
2021-08-12 16:10 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 16:24 ` Kevin Vigouroux via Users list for the GNU Emacs text editor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AM9PR09MB49775144840A291EB047B99696F89@AM9PR09MB4977.eurprd09.prod.outlook.com \
--to=arthur.miller@live.com \
--cc=help-gnu-emacs@gnu.org \
--cc=tomas@tuxteam.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).