Using "repeat" in write/append/lines
[1/9] from: ml::sproingle::com at: 4-May-2004 14:49
I am adding lines to a text file and it is one of those things that I want to print out
as many asterisks as is listed in a variable to make a simple graph. So here is the
effect I am after:
2pm : ****
3pm : **
4pm : *********
The thing is how do I "print" those asterisks when I am outputting to file. I don't know
what to put in the repeat function.
When trying out this on the console, I would try a command like:
write/append/lines %output.txt repeat i 10 ["*"]
But instead of ********** in the appended line I would get a single *
Can anyone help?
Stuart
[2/9] from: tomc:darkwing:uoregon at: 4-May-2004 12:13
On Tue, 4 May 2004, ML wrote:
> I am adding lines to a text file and it is one of those things that I want to print
out as many asterisks as is listed in a variable to make a simple graph. So here is
the effect I am after:
> 2pm : ****
<<quoted lines omitted: 3>>
> When trying out this on the console, I would try a command like:
> write/append/lines %output.txt repeat i 10 ["*"]
repeat i 10 [write/append/lines %output.txt "*"]
should do it
[3/9] from: maximo:meteorstudios at: 4-May-2004 15:20
count: 10
a: copy "" insert/dup tail a "*" count
print a "**********"
OR
full-bar: "****************************************************"
copy/part full-bar count
I find the second less sexy but easier to read inside code.
HTH
MAx
[4/9] from: didec:tiscali at: 4-May-2004 21:48
> I am adding lines to a text file and it is one of those things that I want to print
out as many asterisks as is listed in a variable to make a simple graph. So here is
the effect I am after:
> 2pm : ****
<<quoted lines omitted: 3>>
> When trying out this on the console, I would try a command like:
> write/append/lines %output.txt repeat i 10 ["*"]
I think you can't use repeat like this as it doesn't return the value you want.
But insert/dup should be your friend here :
write/append/lines %output.txt insert/dup copy "" "*" nb ; with "nb" the number of
*
If you want to produce the graphic above, consider generating the line before adding
it to the file :
nb: 4
line: copy "2pm : "
insert/dup line "*" nb
write/append/line %output.txt line
Regards
DideC
[5/9] from: maximo:meteorstudios at: 4-May-2004 15:46
oops
should have been:
>> count: 10
>> a: copy "" insert/dup tail a "*" count
>> a
== "**********"
-MAx
[6/9] from: greggirwin:mindspring at: 4-May-2004 14:25
Hi Stuart,
Tom gave you the best general answer, using REPEAT *around* the calls
you want to make, so I'll just chime in with a note.
Disks are faster these days, but you probably don't want to use WRITE
repeatedly if you're going to be making ten thousand calls that way.
Instead, build up a string buffer and write the whole thing out at
once, or in sections.
INSERT/DUP handily creates a filled string:
head insert/dup clear "" #"*" 10
which you could then wrap up in a function (named for your use here):
ascii-bar: func [len /with fill] [
head insert/dup clear "" any [fill #"*"] len
]
>> ascii-bar 5
== "*****"
>> ascii-bar 15
== "***************"
>> ascii-bar/with 15 "."
== "..............."
>> ascii-bar/with 15 ".-"
== ".-.-.-.-.-.-.-.-.-.-.-.-.-.-.-"
Here's the function, broken down.
head ;- INSERT returns at the point *after* the
insert/dup ; insertion, so we use HEAD to get the top.
clear "" ;* Read up on how local series values are
; retained in functions.
any [ ;- ANY is like a list of OR clauses
fill ;- If FILL (e.g.) is none, it will go on to
#"*" ; the next item in the block, until it finds
] ; a non-none/non-false value.
len ;- This is the value for the DUP refinement
HTH!
-- Gregg
[7/9] from: greggirwin:mindspring at: 4-May-2004 15:12
Hi Stuart,
Addendum to my last post. I made a note about CLEAR, but not that I
didn't use COPY in the func. If you plan to keep the values around and
work on them, you can add it to the func, or use COPY on the results.
-- Gregg
[8/9] from: ml:sproingle at: 5-May-2004 5:43
<<repeat i 10 [write/append/lines %output.txt "*"]
should do it>>
Tom, what that did was print a single * on consecutive lines like this:
*
*
*
*
*
*
*
*
*
*
Instead of having them on a single line like this **********
Stuart
[9/9] from: lmecir:mbox:vol:cz at: 5-May-2004 15:17
Hi Stuart:
>> repeat i 10 [write/append/lines %output.txt "*"]
>>
<<quoted lines omitted: 11>>
>>>
>>>
try:
repeat i 10 [write/append %output.txt "*"]
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted