There's a format directive to zero-pad digits.
cl-user> (format nil "~12,'0d" 27)
"000000000027"
and there's a similar-ish directive to left-align strings while padding them
cl-user> (format nil "~12#<~d~>" 27)
"27 "
Is there a way to do both? That is:
cl-user> (format nil "~12,something,d" 27)
"270000000000"
The naive "~12,'0#<~d~>" does not seem to do what I want here.
cl-user> (format nil "~12,'0#<~d~>" 27)
"27 "
You're close with the last example, but you need some more commas, because tilde less-than takes four arguments, and the pad char is the fourth arguments, but you're passing it as the second. Just pass it as the fourth:
CL-USER> (format nil "~12,,,'0#<~d~>" 27)
"270000000000"
As an aside, it was pointed out in the comments that right padding changes the value that that doesn't seem like a useful operation. I'd say that it can be a useful operation. It might depend on whether these are integers or strings where the values happen to be digit characters. I've seen maintenance systems where upgrades have changed field width and the procedure for migrating old records is to right pad with 0's. The right padding was precisely because it changes the value. 000027 (six chars) can be written as 27, which isn't six chars wide, and 000027 could also be accidentally read (probably by machine, when a programmer isn't careful) as an octal. 270000, on the other hand, has to be six-digits, and won't be octal, since it doesn't start with a 0
Use ~A:
(format nil "~33,,,'0A" 27)
==> "270000000000000000000000000000000"
Related
So I'm trying to print out a list that looks a little bit something like this (setq lst (list '- '- '- '- '-)) and in the past I used the print command to print out the whole list, however, when printing the whole list there is parenthesis on each side which I do not want to see. I want to use something like (format t) to print every bit of my list and I have something like this set up.
(loop for item from 0 to 4
do (progn
(format t "~X" (nth item lst))
)
)
This code prints out the list perfectly fine like this, ----- but as a mentioned, I want it to print spaces between each element so that it is output like this - - - - -. I used the conditional "~X" because I looked up how to output spaces with the format command and you are apparently supposed to use "~X" but it does not work so if anybody knows how I could put spaces between elements that would be greatly appreciated.
Why not just use the features provided by format:
CL-USER> (defvar *my-list* '(- - - -))
*MY-LIST*
CL-USER> (format nil "~{~A~^ ~}" *my-list*)
"- - - -"
CL-USER> (format t "~{~A~^ ~}" *my-list*)
- - - -
NIL
Here the first call to format outputs to a string to show where the spaces are placed. ~{ is an iteration directive that takes a list as its argument. The directives between the opening ~{ and closing ~} are used repeatedly as a format string for the elements of the input list. The ~^ directive causes an early escape from the iteration context when there are no more arguments; this prevents a trailing space from being added.
The second call to format just outputs to *standard-output*.
Regarding your update, that you posted in the answers to your own post:
First of all, you should edit your post to show us that you found a solution, rather than having us look through all the answers to see how much progress you made on your initial problem.
As it was already mentioned in another answer, you can iterate through the elements of a list using format built-in syntax with ~{~} and ~^ (see the documentation !)
In your own solution, when you iterate over the list using loop, you can put a space at the end of the format string rather than calling format twice ...
You can use loop for <elt> in <list> rather than iterating with the indices, and calling nth at each step - which is slower, and also more verbose.
The loop ... do <stuff> already wraps the <stuff> in what we call an implicit progn, i.e. you do not need to wrap yourself all your instructions in a progn, the loop macro does that for you.
There also exists the macro dolist, which is (arguably) simpler to use in those case when you simply want to iterate over a list.
To be fair, it looks like you are a Common Lisp beginner. In this case, I suggest you read the excellent Practical Common Lisp book, which covers in details the loop macro, the format function, and a lot of basic principles. It is available for free online, and is often recommended to beginners, for good reasons !
Ok I came up with an ingenius solution to my problem which I definitely should've seen before.
(loop for item from 0 to 4
do (progn
(format t "~X" (nth item lst))
(format t " ")
)
)
I didn't realize I could print a space like that but it works perfectly fine. Sorry for wasting you all's time but hopefully someone else can see this if they are having a brain fart like me and thanks to everyone who tried to help.
The following cl-ppcre regular expression generates an error:
(ppcre:scan-to-strings "\[([a-zA-Z0-9_-]+)]" "[has-instance]")
debugger invoked on a CL-PPCRE:PPCRE-SYNTAX-ERROR in thread
#<THREAD "main thread" RUNNING {10010B0523}>:
Expected end of string. at position 16 in string "[([a-zA-Z0-9_-]+)]"
What I was expecting as return values is:
“[has-instance]”
#(“has-instance”)
in order to get at the string within the brackets. Can someone provide a regex correction? Thanks.
The escape character (backslash) only escapes itself and double quotes (§2.4.5 Double-Quote):
If a single escape character is seen, the single escape character is discarded, the next character is accumulated, and accumulation continues.
That means that:
"\[([a-zA-Z0-9_-]+)]"
is parsed the same as the following, where backslash is not present:
"[([a-zA-Z0-9_-]+)]"
The PCRE syntax implemented by CL-PPCRE understands the opening square bracket as a special syntax for character classes, and ends at the next closing bracket.
Thus, the above reads the following as a class:
[([a-zA-Z0-9_-]
The corresponding regex tree is:
CL-USER> (ppcre:parse-string "[([a-zA-Z0-9_-]")
(:CHAR-CLASS #\( #\[ (:RANGE #\a #\z) (:RANGE #\A #\Z) (:RANGE #\0 #\9) #\_ #\-)
Note in particular that the opening parenthesis inside it is treated literally. When the parser encounters the closing parenthesis that follows the above fragment, it interprets it as the end of a register group, but no such group was started, hence the error message at position 16 of the string.
To avoid treating the bracket as a character class, it must be preceded by a literal backslash in the string, as you tried to do, but in order to do so you must write two backslash characters:
CL-USER> (ppcre:parse-string "\\[([a-zA-Z0-9_-]+)]")
(:SEQUENCE #\[
(:REGISTER
(:GREEDY-REPETITION 1 NIL
(:CHAR-CLASS (:RANGE #\a #\z) (:RANGE #\A #\Z) (:RANGE #\0 #\9) #\_ #\-)))
#\])
The closing square brackets needs no backslash.
I encourage you to write regular expressions in Lisp using the tree form, with :regex terms when it improves clarity: it avoids having to deal with the kind of problems that escaping brings. For example:
CL-USER> (ppcre:scan-to-strings
'(:sequence "[" (:register (:regex "[a-zA-Z0-9_-]+")) "]")
"[has-instance]")
"[has-instance]"
#("has-instance")
Double escape the square brackets.
You forgot to (double) escape the closing bracket, too.
(cl-ppcre:scan-to-strings "\\[([a-zA-Z0-9_-]+)\\]" "[has-instance]")
;; "[has-instance]" ;
;; #("has-instance")
For those who are new to common lisp, you import cl-ppcre using quicklisp:
(load "~/quicklisp/setup.list") ;; adjust path to where you installed your quicklisp
(ql:quickload :cl-ppcre)
I would like to generate sequential keys that I can use across a number of hash tables. I will call them 'id1','id2' etc. If ht is my hash table then I would like to make symbols from strings as keys. To add an entry to the hash table I want to so something like:
(setf (gethash (make-symbol "id1") ht) 1)
And then access it again with
(gethash 'id1 ht)
I don't think make-symbol is giving me what I want, and the key 'id1' sn't recognised.
What is the best way to make this key?
Error: symbol should be in a package and needs the correct case
In your case we have:
CL-USER 24 > (symbol-name (make-symbol "id0"))
"id0"
CL-USER 25 > (symbol-package (make-symbol "id0"))
NIL
Make sure that you think about the following:
intern the symbol in a package
intern the symbol in the correct package
make sure the symbol has the correct name with the correct case
write symbols with the case you intend to use, possibly you need to escape the symbol to preserve the case
Examples:
uppercased symbol and lowercase symbol name -> not eq
CL-USER 26 > (eq 'id0 (intern "id0" "CL-USER"))
NIL
uppercased symbol and uppercase symbol name -> is eq
CL-USER 27 > (eq 'id0 (intern "ID0" "CL-USER"))
T
an escaped&lowercase symbol and a lowercase symbol name -> is eq
CL-USER 28 > (eq '|id0| (intern "id0" "CL-USER"))
T
make-symbol creates uninterned symbols. It means you will have a unique symbol every time. To get an interned symbol use intern instead.
I want a dynamic way of telling FORMAT to discard output depending an a certain global variable set before the actual call. I figured that changing t to nil should do the deal, but I am not satisfied as I will then not be able to use those FORMAT calls at any point where the returned string could be confused for an actual return value.
E.g:
Telling FORMAT to output on TERMINAL-IO (works fine)
(let ((*the-var* t))
(FORMAT *the-var* "some text")
#|do some other stuff|#)
->"some-text"
->'return-value'
Telling FORMAT to discard output (works fine)
(let ((*the-var* nil))
(FORMAT *the-var* "some text")
#|do some other stuff|#)
->'return-value'
Telling FORMAT to discard output (does not work fine as the returned string of FORMAT might get confused with a possible return value)
(let ((*the-var* nil)) ;no return value intended//nil expected
#|do some stuff|#
(FORMAT *the-var* "some text"))
->"some text"
Therefore I wonder if there is any way of telling FORMAT to discard output without to much fuss, like setting the *the-var* variable to a "/dev/null"-stream or putting a condition-clause around it?
A broadcast stream with no component streams is the Common Lisp way to discard output. You can create one with make-broadcast-stream.
FORMAT does not discard output.
If you pass NIL to FORMAT as output direction, then it will return the output as a string and will not print to a stream.
The best way to not print anything is to not call FORMAT.
It makes very little sense to use FORMAT to generate output and not use that output for display. Just check if you want output or not.
(let ((output-p nil)) ;no return value intended//nil expected
#|do some stuff|#
(when output-p
(FORMAT stream "some text")))
I am using GNU CLISP and getting crazy because this program:
(read-line)
(format t "~&<prompt RESPONSE")
Outputs something like:
my input...
<prompt RESPONSE
But if I print a prompt before (read-line) I get a spurious empty line:
(format t "~&prompt> ")
(read-line)
(format t "~&<prompt RESPONSE")
E.g.:
prompt> my input...
<prompt RESPONSE
I have tried using (finish-output) both before and after read-line, but it didn't help. I do not understand where that empty line come from. I suspect there is a bug in CLISP.
For those who don't know, ~& in a format command is a conditional newline, which means "print a newline if need be". That is, only print a newline if the last thing lisp printed wasn't already a newline.
The format function (at least on your machine!) ignores user input when deciding whether to issue a newline. This may not be the case on all systems. I really don't remember.
Let's focus on the ~& that immediately precedes <prompt RESPONSE. In your first case, format sees that it hasn't printed anything yet and thus does not need to print a newline. In the second case format sees that the last thing it printed was not a newline and therefore it needs to print a newline.
The solution: Knowing that the user will always provide the newline, don't include it in your format statement. You can even remove it from your initial prompt if you wish:
(format t "prompt> ")
(read-line)
(format t "<prompt RESPONSE")
So, then the question arises, "Then what is ~& for anyhow?" And, indeed, there are other uses for it. For example, to separate 2 consecutive lines of output:
(format t "prompt> ")
(read-line)
(format t "<prompt RESPONSE 1~&<prompt RESPONSE 2")