Variable-length foreach block of words
[1/1] from: pa::russo::perd::com at: 30-Sep-2000 23:36
--============_-1241772622==_============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"
Hi all!
You can find attached a little (in)utility that contains a couple of
interesting issues (at least, I believe so!) .
You can find an example of a variable-length block of words supplied
to the 'foreach' construct and the use of 'bind' to refer the single
words in an inner 'foreach' loop.
Since the context-binding issue is a bit hard one (to me, at least),
I think this script could be useful to some beginner... in addiction
to the enlightening reading of "REBOL, The Official Guide" ;) (BTW:
great job, Elan. Really!)
Obviously, I'll be very happy to learn something more, if someone
tells me I'm totally dumb and that it exists a far simpler way to
accomplish the same task. ;)
In particular, I'm not happy about the way I had to force a result
for the second 'try' clause in the no-error case to prevent 'error'
to remain undefined.
Here's the function, while the file attached contains a complete example.
break-textfile: func [
{It breaks a main text-file in a number of text-files with a
fixed number of lines.
It returns the created files' number or a standard error object.}
source-filename [string!] "The file to break"
prefix [string!] "A seed for the files' name"
storage-directory [string!] "The name of the storage directory"
number-of-citizens-per-file [integer!] "The number of lines per file"
/local counter citizens-group postfix
][
if error? error: try [citizens: read/lines to-file
source-filename] [ return disarm error]
counter: 0
citizens-group: []
for i 1 number-of-citizens-per-file 1 [
append citizens-group to-word join "a" i
]
foreach :citizens-group citizens [
bind citizens-group 'a1
postfix: head either (length? to-string counter) = 1[insert
to-string counter "0"][to-string counter]
if error? error: try [
if not exists? join %"" storage-directory [make-dir
join %"" storage-directory]
filename: join %"" [storage-directory "/" prefix postfix]
if exists? filename [delete filename]
foreach citizen citizens-group [
if not none? get citizen [write/append filename
rejoin [ get citizen newline]]
]
"dummy value for 'try' to return"
] [
return disarm error
]
counter: counter + 1
]
return counter
]
--============_-1241772622==_============
Content-Id: <a05001901b5fc08743982@[151.15.108.177].0.0>
Content-Type: text/plain; name="textfile-breaker.r"; charset="us-ascii" ; format="flowed"
Content-Disposition: attachment; filename="textfile-breaker.r"
; modification-date="Sat, 30 Sep 2000 23:15:20 +0200"
REBOL [
Title: "Text-File-breaker"
Date: 30-sep-2000
Author: "Paolo Russo"
Owner: "PERD s.r.l."
Site: http://www.perd.com
Email: [pa--russo--perd--com]
File: %textfile-breaker.r
Version: 1.0.0
Project: 'srr
Purpose: {
It breaks a main text-file in a set of text-files with a
fixed number of lines.
}
Comments: {
This script contains a couple of interesting issues (At
least, I believe so!) .
You can see an example of a variable-length block of words
for the 'foreach' construct.
In particular you can observe the use of 'bind' to refer the
single words using a reference to the block
in an inner 'foreach' loop.
}
History: [
1.0.0 30-sep-2000 "Main layout" "Paolo Russo"
]
Category: [script utility]
Needs: [2.2]
Language: 'English
]
break-textfile: func [
{It breaks a main text-file in a number of text-files with a
fixed number of lines.
It returns the created files' number or a standard error object.}
source-filename [string!] "The file to break"
prefix [string!] "A seed for the files' name"
storage-directory [string!] "The name of the storage directory"
number-of-citizens-per-file [integer!] "The number of lines per file"
/local counter citizens-group postfix
][
if error? error: try [citizens: read/lines to-file
source-filename] [return disarm error]
counter: 0
citizens-group: []
for i 1 number-of-citizens-per-file 1 [
append citizens-group to-word join "a" i
]
foreach :citizens-group citizens [
bind citizens-group 'a1
postfix: head either (length? to-string counter) = 1[insert
to-string counter "0"][to-string counter]
if error? error: try [
if not exists? join %"" storage-directory [make-dir
join %"" storage-directory]
filename: join %"" [storage-directory "/" prefix postfix]
if exists? filename [delete filename]
foreach citizen citizens-group [
if not none? get citizen [write/append filename
rejoin [ get citizen newline]]
]
"dummy value for 'try' to return"
] [
return disarm error
]
counter: counter + 1
]
return counter
]
; Collect params
prefix: ask "Prefix for files to create: "
source-filename: ask "Complete path to the file to split: "
storage-directory: ask "Name of the storage directory: "
until [
all [
not error? try [ number-of-lines-per-file: to-integer ask
Number of lines per file [ > 0 ]:
]
number-of-lines-per-file > 0
]
]
; Execute function
probe break-textfile source-filename prefix storage-directory
number-of-lines-per-file
--============_-1241772622==_============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"
--
Paolo Russo
[pa--russo--perd--com]
_________________
PERD s.r.l.
Virtual Technologies for Real Solutions
http://www.perd.com
--============_-1241772622==_============--