Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[Nifty Function of the Day] Pad

 [1/14] from: AJMartin::orcon::net::nz at: 15-Aug-2003 21:53


With acknowledgements to Bohdan "Bo" Lechnowsky: Pad: function [ "Pads a value with leading zeroes or a specified fill character." Value [string! number!] Length [integer!] /Fill Character [char!] "Optional Fill Character". ] [Field] [ if not string? Value [ Value: form Value ] if none? Character [ Character: #"0" ] insert/dup Field: copy "" Character Length change/part Field head reverse Value length? Value head reverse Field ] Andrew J Martin ICQ: 26227169 http://www.rebol.it/Valley/ http://valley.orcon.net.nz/ http://Valley.150m.com/

 [2/14] from: g:santilli:tiscalinet:it at: 15-Aug-2003 12:57


Hi Andrew, On Friday, August 15, 2003, 11:53:18 AM, you wrote: AJM> Pad: function [ AJM> "Pads a value with leading zeroes or a specified fill character." AJM> Value [string! number!] Length [integer!] AJM> /Fill Character [char!] "Optional Fill Character". AJM> ] [Field] [ AJM> if not string? Value [ AJM> Value: form Value AJM> ] AJM> if none? Character [ AJM> Character: #"0" AJM> ] AJM> insert/dup Field: copy "" Character Length AJM> change/part Field head reverse Value length? Value AJM> head reverse Field AJM> ] pad: func [ "Pads a value with leading zeroes or a specified fill character." Value [string! number!] Length [integer!] /Fill Character [char!] "Optional Fill Character." ] [ if not string? Value [Value: form Value] head insert insert/dup make string! 2 + Length any [Character #"0"] Length - length? Value Value ] pad: func [ "Pads a value with leading zeroes or a specified fill character." Value [string! number!] Length [integer!] /Fill Character [char!] "Optional Fill Character." ] [ copy skip insert insert/dup clear "" any [Character #"0"] Length Value negate Length ] (The latter truncates the value too, if it's longer than Length.) Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/

 [3/14] from: dockimbel:free at: 15-Aug-2003 13:17


Hi Andrew, A J Martin wrote:
> With acknowledgements to Bohdan "Bo" Lechnowsky: > Pad: function [
<<quoted lines omitted: 12>>
> head reverse Field > ]
You can avoid testing if Value is a string! in order to FORM it. FORM won't change the value if it's already a string!, it will just COPY it. You can also write a shorter version of 'pad : pad: func [ "Pads a value with leading zeroes or a specified fill character." value [string! number!] n [integer!] /with c [char!] "Optional Fill Character" ][ value: form value head insert/dup value any [all [with c] #"0"] n - length? value ] -DocKimbel

 [4/14] from: amicom:sonic at: 15-Aug-2003 6:09


To everyone who contributed to this thread: Very nice collection of 'pad functions! Bohdan "Bo" Lechnowsky Lechnowsky Technical Consulting At 01:17 PM 8/15/03 +0200, you wrote:

 [5/14] from: amicom:sonic at: 15-Aug-2003 6:13


At 01:17 PM 8/15/03 +0200, you wrote:
>You can also write a shorter version of 'pad : > pad: func [
<<quoted lines omitted: 5>>
> head insert/dup value any [all [with c] #"0"] n - length? value > ]
And an even shorter variation: pad: func [ "Pads a value with leading zeroes or a specified fill character." val [string! number!] n [integer!] /with c [char!] "Optional Fill Character" ][ head insert/dup val: form val any [all [with c] #"0"] n - length? val ] Bohdan "Bo" Lechnowsky Lechnowsky Technical Consulting

 [6/14] from: greggirwin:mindspring at: 15-Aug-2003 8:57


Hi Bo, et al BoRL> And an even shorter variation: And a longer version, with a different name and more features: justify: func [ {Justify the given string to the specified width.} s [string!] "The string to justify" wd [integer!] "The target width, in characters" /left "Left justify the string (default)" /center {Center justify the string. If the total length of the padding is an odd number of characters, the extra character will be on the right.} /right "Right justify the string" /with {Allows you to specify filler other than space. If you specify a string more than 1 character in length, it will be repeated as many times as necessary.} filler [string! char!] "The character, or string, to use as filler" /local pad-len result ][ if 0 >= pad-len: (wd - length? s) [return s] filler: form any [filler " "] result: head insert/dup make string! wd filler wd / length? filler ; If they gave us a multi-char filler, and it isn't evenly multiplied ; into the desired width, we have to add some extra chars at the end ; to make up for the difference. if wd > length? result [ append result copy/part filler wd - length? result ] pos: either center [ add 1 to integer! divide pad-len 2 ][ either right [add 1 pad-len][1] ] head change/part at result pos s length? s ] -- Gregg

 [7/14] from: AJMartin:orcon at: 16-Aug-2003 10:04


And an even shorter version: Pad: func [ "Pads a value with leading zeroes or a specified fill character." Value [any-type!] "The value." Width [integer!] "The desired width." /With Fill [char!] "With the specified Fill character." ] [ head insert/dup Value: form Value any [Fill #"0"] Width - length? Value ] Thanks to Bo, DocKimbel, Gabriele and Gregg! Andrew J Martin ICQ: 26227169 http://www.rebol.it/Valley/ http://valley.orcon.net.nz/ http://Valley.150m.com/

 [8/14] from: atruter:labyrinth:au at: 16-Aug-2003 19:11


How about one that justifies based on type? pad: func [value width] [ either any [number? value money? value date? value] [ head insert/dup value: form value " " width - length? value ][ head insert/dup tail value: form value " " width - length? value ] ] Regards, Ashley

 [9/14] from: greggirwin:mindspring at: 16-Aug-2003 9:37


Hi Ashley, AT> How about one that justifies based on type? AT> pad: func [value width] [ AT> either any [number? value money? value date? value] [ AT> head insert/dup value: form value " " width - length? value AT> ][ AT> head insert/dup tail value: form value " " width - length? value AT> ] AT> ] You can remove the redundancy in that code like this: pad: func [value width /local op] [ op: either any [number? value money? value date? value] [:head][:tail] head insert/dup op value: form value " " width - length? value ] -- Gregg

 [10/14] from: greggirwin:mindspring at: 16-Aug-2003 10:01


GI> pad: func [value width /local op] [ GI> op: either any [number? value money? value date? value] [:head][:tail] GI> head insert/dup op value: form value " " width - length? value GI> ] Or maybe this will work for you: pad: func [value width /local op] [ op: either any-string? value [:tail][:head] head insert/dup op value: form value " " width - length? value ] -- Gregg

 [11/14] from: brian:hawley at: 16-Aug-2003 14:03


At 10:01 AM 8/16/03 -0600, Greg Irwin wrote:
>GI> pad: func [value width /local op] [ >GI> op: either any [number? value money? value date? value] [:head][:tail]
<<quoted lines omitted: 5>>
> head insert/dup op value: form value " " width - length? value >]
Or maybe this (safer and without locals): pad: func [value width [number!]] [ head insert/dup do pick [:tail :head] any-string? :value value: form :value " " width - length? value ] - Brian Hawley

 [12/14] from: AJMartin:orcon at: 17-Aug-2003 8:45


Or one that switches around the fill character depending upon the value's type: Pad: function [ "Pads a value with leading zeroes or trailing spaces or a specified fill character." Value [any-type!] "The value." Width [integer!] "The desired width." /With Fill [char!] "With the specified Fill character." ] [ Operator ] [ Operator: either any-string? Value [ any [Fill Fill: #" "] :tail ] [ any [Fill Fill: #"0"] :head ] head insert/dup Operator Value: form Value Fill Width - length? Value ] Andrew J Martin ICQ: 26227169 http://www.rebol.it/Valley/ http://valley.orcon.net.nz/ http://Valley.150m.com/

 [13/14] from: atruter:labyrinth:au at: 18-Aug-2003 10:54


> pad: func [value width /local op] [ > op: either any-string? value [:tail][:head]
<<quoted lines omitted: 5>>
> " " width - length? value > ]
Thanks guys, a couple more usefull additions to the REBOL bag of tricks (I'd forgotten about any-string? and insert head, "do pick" is a new one for me) ;) Regards, Ashley

 [14/14] from: brian:hawley at: 18-Aug-2003 11:07


At 10:54 AM 8/18/03 +1000, Ashley Truter wrote:
>Thanks guys, a couple more usefull additions to the REBOL bag of tricks >(I'd forgotten about any-string? and insert head, "do pick" is a new one >for me) ;)
Do either would have worked too, but pick is smaller and more efficient in the simple case of returning a value. The do here is just like apply in some other languages. Pardon me if you already knew figured this out... :) Enjoy! Brian

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted