Best method to create fixed length strings

Started by ale870, August 21, 2008, 04:17:57 PM

Previous topic - Next topic

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!
--

HPW

#1
When your input are only numbers then how about:



>(replace " "(format "%-4s" "12")"-")
"12--"


For other input containing a space a regex would do it:



>(replace " +\z" (format "%-8s" "12 12") (dup "-" (length $0)) 0)
"12 12---"
Hans-Peter

DrDave

#2
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.
...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

cormullion

#3
Hi Alessandro! How about this:


(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:


(0 4 (string n "----"))

(select (append (string n) "----")  (sequence 0 3)))

(join (array-list (array 4 (explode (string n (dup "-" 4))))))

Good fun for a Friday afternoon... :)

xytroxon

#4

> (setq s1 12)
12
> (slice (string s1 "----") 0 4)
"12--"


Q.E.D. ;)



--xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

DrDave

#5
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:


(define (make4 s1)
  (0 4 (string s1 "----")))

usage:
(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.
...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

ale870

#6
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!!!
--

ale870

#7
Hello DrDave, your function is good, so I expanded it!


(define (make-n s1 argCount)
    (0 argCount (string s1 (dup "-" argCount) ) ) )


Usage:





> (make-n 123 5)
"123--"
> (make-n 123 5)
"123--"


:-)
--

DrDave

#8
Alessandro,



Let's go another step towards more generic:


(define (make-n s1 argCount (filler "-"))
    (0 argCount (string s1 (dup filler argCount))))

usage:
>(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

ale870

#9
Yes! I like it! I will use it in my app!
--