Page 1 of 1
Best method to create fixed length strings
Posted: Thu Aug 21, 2008 11:17 pm
by ale870
Hello,
I need to make fixed length strings, starting from variable length strings.
Example:
"12" -> "12--" (4 characters in total)
"1" -> "1---"
"1234 -> "1234"
I have a solution but I think it is good for imperative programming, and it is not a good solution for functional languages.
Can you give me some suggestions and/or show me some small algorithms to make it in a "functional" fashion?
Thank you!
Posted: Fri Aug 22, 2008 5:35 am
by HPW
When your input are only numbers then how about:
Code: Select all
>(replace " "(format "%-4s" "12")"-")
"12--"
For other input containing a space a regex would do it:
Code: Select all
>(replace " +\\z" (format "%-8s" "12 12") (dup "-" (length $0)) 0)
"12 12---"
Re: Best method to create fixed length strings
Posted: Fri Aug 22, 2008 8:25 am
by DrDave
Here are the functions that can help you. I think you'll see how to make use of them to create a function that takes as parameters your input string and a filler string and returns a string of length 4.
Note that s1 and filler aren't restricted to just strings but can really be almost anything you want: numbers, strings, expressions.
(set 'filler "----") //assume max length of s1 is 4
(set 's1 "12")
(set 'new-string (string s1 filler) //concatenates s1 and filler into a string
(slice new-string 0 4) //copies first 4 characters as a string and leaves new-string
(0 4 new-string) //implicit SLICE
So, if s1 is the empty string, you'll get the filler string returned. Otherwise, you'll get your starting string padded with filler but truncated to the first 4 characters.
Posted: Fri Aug 22, 2008 3:39 pm
by cormullion
Hi Alessandro! How about this:
Code: Select all
(set 'buffer "")
(for (i 0 3)
(if (< i (length (string n)))
(write-buffer buffer (nth i (string n)))
(write-buffer buffer "-")))
(println "result " buffer)
Ahh. Perhaps not. :) HPW and DrDave have more newLISP-y ways, I think. Variations on a theme:
Code: Select all
(select (append (string n) "----") (sequence 0 3)))
Code: Select all
(join (array-list (array 4 (explode (string n (dup "-" 4))))))
Good fun for a Friday afternoon... :)
Posted: Fri Aug 22, 2008 6:16 pm
by xytroxon
Code: Select all
> (setq s1 12)
12
> (slice (string s1 "----") 0 4)
"12--"
Q.E.D. ;)
--xytroxon
Posted: Fri Aug 22, 2008 7:11 pm
by DrDave
Uh, guys, he said he wanted it in a "functional" fashion, so I take that to mean he wants to have function to use. So now that we danced all around writing a function, here it is:
Code: Select all
(define (make4 s1)
(0 4 (string s1 "----")))
usage:
Code: Select all
(make4 "ABc") --> "ABc-"
(make4 (+ 3 4)) --> "7---"
(make4 (list 'a)) --> "(a)-"
As I said previously, s1 isn't restricted to being numeric, as he used in his original example, or a string, as we might be tempted to expect, but can be any valid expression. STRING takes care of all the "heavy lifting" of converting the result of s1 to a string and then concatenating it with the filler.
Posted: Fri Aug 22, 2008 7:16 pm
by ale870
Thank you!
Your fantasy is without limits! :-)
Some solutions seems more "traditional" (imperative programming) but other ones are really "functional".
I love the solution proposed by cormullion: (0 4 (string n "----"))
I think newLisp and functional languages are great to solve complex solutions in easy clever ways!
I always need your help since I'm a "imperative programmer" for a long time, and sometimes I find hard to "change my mind" to think in "functional" way!!!
THank you again, everybody helped me so much!!!
Posted: Fri Aug 22, 2008 9:50 pm
by ale870
Hello DrDave, your function is good, so I expanded it!
Code: Select all
(define (make-n s1 argCount)
(0 argCount (string s1 (dup "-" argCount) ) ) )
Usage:
Code: Select all
> (make-n 123 5)
"123--"
> (make-n 123 5)
"123--"
:-)
Posted: Sat Aug 23, 2008 5:31 am
by DrDave
Alessandro,
Let's go another step towards more generic:
Code: Select all
(define (make-n s1 argCount (filler "-"))
(0 argCount (string s1 (dup filler argCount))))
usage:
Code: Select all
>(make-n "ABc" 7) --> "ABc----"
>(make-n "ABc" 7 "+") --> "ABc++++"
>(make-n "AB78" 7 " ") --> "AB78 "
Posted: Sat Aug 23, 2008 1:27 pm
by ale870
Yes! I like it! I will use it in my app!