Best method to create fixed length strings
Best method to create fixed length strings
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!
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!
--
When your input are only numbers then how about:
For other input containing a space a regex would do it:
Code: Select all
>(replace " "(format "%-4s" "12")"-")
"12--"
Code: Select all
>(replace " +\\z" (format "%-8s" "12 12") (dup "-" (length $0)) 0)
"12 12---"
Hans-Peter
Re: Best method to create fixed length strings
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.
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.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Hi Alessandro! How about this:
Ahh. Perhaps not. :) HPW and DrDave have more newLISP-y ways, I think. Variations on a theme:
Good fun for a Friday afternoon... :)
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)
Code: Select all
(0 4 (string n "----"))
Code: Select all
(select (append (string n) "----") (sequence 0 3)))
Code: Select all
(join (array-list (array 4 (explode (string n (dup "-" 4))))))
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:
usage:
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.
Code: Select all
(define (make4 s1)
(0 4 (string s1 "----")))
Code: Select all
(make4 "ABc") --> "ABc-"
(make4 (+ 3 4)) --> "7---"
(make4 (list 'a)) --> "(a)-"
Last edited by DrDave on Fri Aug 22, 2008 7:17 pm, edited 1 time in total.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2
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!!!
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!!!
--
Hello DrDave, your function is good, so I expanded it!
Usage:
:-)
Code: Select all
(define (make-n s1 argCount)
(0 argCount (string s1 (dup "-" argCount) ) ) )
Code: Select all
> (make-n 123 5)
"123--"
> (make-n 123 5)
"123--"
--
Alessandro,
Let's go another step towards more generic:
usage:
Let's go another step towards more generic:
Code: Select all
(define (make-n s1 argCount (filler "-"))
(0 argCount (string s1 (dup filler argCount))))
Code: Select all
>(make-n "ABc" 7) --> "ABc----"
>(make-n "ABc" 7 "+") --> "ABc++++"
>(make-n "AB78" 7 " ") --> "AB78 "
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2