[REBOL] Re: [append][series]Appending to a series of strings
From: joel:neely:fedex at: 18-Nov-2003 11:07
Hi, Seth,
Seth wrote:
> Joel Neely wrote:
>>...you can't treat that (non-existent) element as a series.
>>
>>Unlike Perl, which automatically allocates and meaningfully initializes
>>previously non-existent data, REBOL requires that a value exist and be
>>of the correct type for whatever operation you attempt to perform on it.
>
> Thanks everyone... I was coming from a Perl point of view here -- The
> REBOL way is a lot more logical -- This is what I get for coding at
> ungodly hours in the morning ;] Thanks... :D
>
IMHO neither more nor less logical, just differently logical.
Suppose one has a collection of small natural numbers (such as
test scores ranging from 0 to 100) and one wants to know how many
occurrences of each distinct number there are.
Using Perl arrays:
# assume @scores contains the raw data with dups
@tallies = ();
foreach $score (@scores) {
++$tallies[$score];
}
foreach $score (0..$#tallies) {
print "$score: $tallies[$score]\n" if $tallies[$score];
}
Using REBOL blocks:
; assume SCORES contains the raw data with dups
tallies: []
foreach score scores [
insert/dup tail tallies 0 score + 1 - length? tallies
change at tallies score + 1 1 + pick tallies score + 1
]
forall tallies [
if 0 < tallies/1 [print [-1 + index? tallies ":" tallies/1]]
]
or
; assume SCORES contains the raw data with dups
tallies: []
foreach score scores [
either found? here: select tallies score [
here/1: here/1 + 1
][
append tallies reduce [score copy [1]]
]
]
foreach [score tally] sort/skip tallies 2 [
print [score ";" tally/1]
]
REBOL is much more "literal"; there are no values that one does not
explicitly create (although it is possible to be implicitly explicit
at times ;-). On the other hand, it is necessary explicitly to
manage details that aren't at the same logical level as the original
problem (making sure that there enough "places" to store the next
tally needed, etc).
I'd be interested in any *self-contained* solutions to the above task
that might be clearer than the above.
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
Enron Accountingg in a Nutshell: 1c=$0.01=($0.10)**2=(10c)**2=100c=$1