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

Constant size string variable?

 [1/12] from: bpaddock::csonline::net at: 1-Dec-2001 17:03


Once again I'm working on my patent downloading script, that you can now find in the library. I need a string variable that is always ten characters long. A variable number of digits or letters from 1 to 10 might be entered in a dialog box. If less than ten are entered those character positions are filled with a plus sign (+) preceding the numbers. Examples: ++++++++++ [10 of them] +++++++++1 ++++++4321 +987654321 What is the best way to do this in Rebol?

 [2/12] from: gchiu:compkarori at: 2-Dec-2001 11:58


On Sat, 1 Dec 2001 17:03:38 -0500 Bob Paddock <[bpaddock--csonline--net]> wrote:
> ++++++++++ [10 of them] > +++++++++1 > ++++++4321 > +987654321 > > What is the best way to do this in Rebol?
You could try joining the 10 + infront, and the copying the last 10 chars? -- Graham Chiu

 [3/12] from: greggirwin:mindspring at: 1-Dec-2001 15:56


Hi Bob, << I need a string variable that is always ten characters long. ...If less than ten are entered those character positions are filled with a plus sign (+) preceding the numbers. >> I don't know if this is the best way, but it should work. Watch for line wrap! mk-string: func [n[integer!] ch[string!]][to-string array/initial n ch] justify: func [ {Justify the given string to the specified width. Uses: mk-string left} 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 pad-str half-pad ][ if (length? s) >= wd [return s] if not with [filler: " "] pad-len: wd - length? s pad-str: copy/part (mk-string pad-len filler) pad-len either center [ ; rejoin [pad s pad (extra-char?)] half-pad: copy/part pad-str to-integer (pad-len / 2) rejoin [ half-pad s half-pad ; This is getting longer and uglier by the minute. It may be an odd ; number of fill chars, and they may give us a single char. ;either pad-len // 2 = 0 [""][pick filler (length? half-pad) + 1] either (pad-len // 2 = 0) [ "" ][ either ((length? filler) > 1) [ pick filler (length? half-pad) + 1 ][ filler ] ] ] ][ rejoin either right [[pad-str s]][[s pad-str]] ] ]
>> justify/right/with "" 10 "+"
== "++++++++++"
>> justify/right/with "1" 10 "+"
== "+++++++++1"
>> justify/right/with "4321" 10 "+"
== "++++++4321"
>> justify/right/with "987654321" 10 "+"
== "+987654321" HTH! --Gregg

 [4/12] from: bpaddock:csonline at: 1-Dec-2001 18:18


> > ++++++++++ [10 of them] > > +++++++++1
<<quoted lines omitted: 3>>
> > What is the best way to do this in Rebol? > You could try joining the 10 + infront,
That part was not hard to figure out: plus_pn: probe rejoin[ "++++++++++" PatentNumber ]
> and the copying the last 10 chars?
But how do I get the last 10, not the first 10? plus_pn: probe copy/part/tail plus_pn 10 That doesn't work because copy has no /tail refinment.

 [5/12] from: brett:codeconscious at: 2-Dec-2001 10:41


Hi Bob,
> A variable number of digits or letters from 1 to 10 might be entered in > a dialog box. > > If less than ten are entered those character positions are filled with a
plus
> sign (+) preceding the numbers.
If ustr is the entered string: ustr: {12} You could copy as much of a filler string as you need: pstr: {++++++++++} join copy/part pstr subtract length? pstr length? ustr ustr Or insert as many + characters as you need: head insert/dup copy ustr #"+" subtract 10 length? ustr Brett.

 [6/12] from: larry:ecotope at: 1-Dec-2001 16:05


Hi Bob,
> But how do I get the last 10, not the first 10? > > plus_pn: probe copy/part/tail plus_pn 10 > > That doesn't work because copy has no /tail refinment.
To get the last 10 elements of a series: plus_pn: copy skip tail plus_pn -10 Brett has given another solution, but you may find this handy anyway. -Larry

 [7/12] from: carl:cybercraft at: 2-Dec-2001 13:28


How about...
>> head reverse head change "++++++++++" head reverse "123"
== "+++++++123" Or perhaps not... (: -- Carl Read

 [8/12] from: tomc:darkwing:uoregon at: 1-Dec-2001 19:12


pad: "++++++++++" == "++++++++++"
>> str: "123"
== "123"
>> insert str skip pad (length? str)
== "123"
>> str
== "+++++++123" seems ok On Sat, 1 Dec 2001, Bob Paddock wrote:

 [9/12] from: ingo:2b1 at: 2-Dec-2001 12:01


Hi Bob, Once upon a time Bob Paddock spoketh thus: <..>
> I need a string variable that is always ten characters long.
<..>
> Examples: > > ++++++++++ [10 of them] > +++++++++1 > ++++++4321 > +987654321
Here're some ideas to get you going ...
>> ; Version 1 >> fillers: "++++++++++"
== "++++++++++"
>> str: "677"
== "677"
>> pos: (length? f) - (length? str) + 1
== 8
>> f: copy fillers
== "++++++++++"
>> change at f pos str
== ""
>> f
== "+++++++677"
>> ; Version 2 >> len: (length? f) - (length? str)
== 7
>> rejoin [ copy/part fillers len str ]
== "+++++++677"
>> ; Version 3 >> copy skip tail join fillers str -10
== "+++++++677" I hope that helps, Ingo

 [10/12] from: ljurado:bariloche:ar at: 2-Dec-2001 20:01


> > ++++++4321 > > What is the best way to do this in Rebol?
This ? ... pad/right/with input 10 #"+" 4321 == "++++++4321" Luis.

 [11/12] from: brett:codeconscious at: 3-Dec-2001 12:57


Hi Luis,
> pad/right/with input 10 #"+" > 4321 > == "++++++4321"
I don't have a PAD function in my installation. Maybe you have some special library called in your user.r ? Brett.

 [12/12] from: ljurado:bariloche:ar at: 2-Dec-2001 23:54


> Hi Luis, > > > pad/right/with input 10 #"+" > > 4321 > > == "++++++4321" > > I don't have a PAD function in my installation. Maybe you have some
special
> library called in your user.r ? > > Brett.
Ooops ... http://anton.idatam.com.au/rebol/library/pad.r From Desktop/Rebol.com/Sites/Anton/The real thing/Setup Anton :-) Luis

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