Best method to create fixed length strings

Notices and updates
Locked
ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Best method to create fixed length strings

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

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post 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---"
Hans-Peter

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Re: Best method to create fixed length strings

Post 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.
...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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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

(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))))))
Good fun for a Friday afternoon... :)

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

Code: Select all

> (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
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post 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.
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

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

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

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post 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--"
:-)
--

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post 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   "
...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
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

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

Locked