[REBOL] Re: Line reduction
From: joel:neely:fedex at: 5-Jul-2001 11:03
Hi, Aaron,
There seems to be a mismatch between your written specification
and the behavior of your sample program. Of course, I blame the
English language, rather than you (or REBOL ;-)!
Aaron Roberts wrote:
> ... It asks for an input file, output file, and the number
> of lines to skip before deleting. If you enter a 1, it will
> delete every other line. A 2 every third line. Etc...
>
...
> REBOL [
> TITLE: "Remove lines"
> DATE: 3-July-01
> ]
>
> input_file: ask "Enter the source file name: "
> output_file: ask "Enter the output file name: "
> skip_amount: ask "Remove lines every X line: "
> datafile: read/lines to-file input_file
>
> forskip datafile to-integer skip_amount[
>
> remove datafile
>
> ]
>
> datafile: head datafile
> write/lines to-file output_file datafile
>
As written, the above script removes the first line, then skips
SKIP_AMOUNT lines, removes the next line, etc...
Using a small test file (lines labeled with uppercase letters,
to avoid zero/one flamage! ;-), we start with datafile.txt
containing:
Line: A
Line: B
Line: C
Line: D
... and so on, through
Line: X
Line: Y
Line: Z
and then
>> do %removelines.r
Enter the source file name: datafile.txt
Enter the output file name: dataout.txt
Remove lines every X line: 1
>> do %removelines.r
Enter the source file name: datafile.txt
Enter the output file name: dataout2.txt
Remove lines every X line: 2
after which dataout.txt contains
Line: B
Line: D
Line: F
... etc.
Line: V
Line: X
Line: Z
and dataout2.txt contains
Line: B
Line: C
Line: E
Line: F
Line: H
... through
Line: U
Line: W
Line: X
Line: Z
If you really want to remove the *last* of every group of lines
(SKIP_AMOUNT + 1 in all), you might consider this (note the
change in the prompt string):
input_file: ask "Enter the source file name: "
output_file: ask "Enter the output file name: "
skip_amount: ask "Remove line after every X lines: "
datafile: read/lines to-file input_file
use [len skp] [
len: length? datafile
skp: 1 + to-integer skip_amount
for rem len - (len // skp) skp (- skp) [
remove at datafile rem
]
]
write/lines to-file output_file datafile
which performs as follows:
>> do %removelast.r
Enter the source file name: datafile.txt
Enter the output file name: lastout1.txt
Remove line after every X lines: 1
>> do %removelast.r
Enter the source file name: datafile.txt
Enter the output file name: lastout2.txt
Remove line after every X lines: 2
with results in lastout1.txt of
Line: A
Line: C
Line: E
... through
Line: U
Line: W
Line: Y
and in lastout2.txt of
Line: A
Line: B
Line: D
Line: E
Line: G
... through
Line: V
Line: W
Line: Y
Line: Z
I don't know your application, but it might be less confusing to
further change the prompt (and script!) to as follows:
;...
skip_amount: ask "Remove last 1 of every X lines: "
;...
use [len skp] [
;...
skp: to-integer skip_amount
;...
]
HTH!
-jn-
--
___________________________________________________________________
The purpose of computing is insight, not numbers!
- R. W. Hamming
joel'dot'neely'at'fedex'dot'com