getting rid of empty string in a block.
[1/8] from: p0665393:magellan:umontreal:ca at: 13-Mar-2002 11:39
Hi all,
I want to sort a file by line alphabetic order. It appears it works at once as I
write an idea ! Rebol is great !
str: copy []
foreach l (sort read/lines %my-file) [append str join l "^/"]
But I haven't thought of blank line which are empty string in the sort
read/lines resulting block.
I try using the string length. But I don't understand the following evaluations
:
>> t: ["" "" "one" "two"]
>> foreach i t [print length? i]
0
0
3
3
>> foreach i t [if length? i [print i]]
one
two
>> foreach i t [if length? i <> 0 [print i]]
** Script Error: length? expected series argument of type: series port tuple
struct
** Near: if length? i <> 0
>> foreach i t [if (length? to-string i <> 0) [print i]]
one
two
----
What would be the best solution to get rid of the empty strings ? (or of any
empty values of a block, be they string or of any type? Could you give me a
pointer to the explanation of this in the official doc ?
Many thanks,
Best regards
Stephane
[2/8] from: rebol665:ifrance at: 13-Mar-2002 18:30
Hi Stéphane
Try this
>> foreach i t [if (length? i) <> 0 [print i]]
one
two
Rebol is smart, but not smart enough to figure out where to stop in that
case : if length? i <> 0
Salut
Patrick
[3/8] from: al:bri:xtra at: 14-Mar-2002 7:31
Stephane wrote:
> str: copy []
> foreach l (sort read/lines %my-file) [append str join l "^/"]
>
> But I haven't thought of blank line which are empty string in the sort
read/lines resulting block.
str: copy []
foreach Line sort read/lines %my-file [
if not empty? Line [
append str join Line newline
]
]
> >> foreach i t [if length? i <> 0 [print i]]
Try:
foreach i t [if 0 <> length? i [print i]]
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[4/8] from: cybarite:sympatico:ca at: 13-Mar-2002 12:01
one way to remove empty strings from a collection is
difference ["A" "" "C" "" "E" ] [""]
but because it is difference, there must be an empty string in the first
collection.
To get around that you can add one then remove all with append or union:
difference union [""] ["A" "C" "E" "" "F" ] [""]
or
difference union [""] read/lines %some-file.txt [""]
[5/8] from: greggirwin:mindspring at: 13-Mar-2002 11:27
Hi Stephane,
<< What would be the best solution to get rid of the empty strings ? (or of
any
empty values of a block, be they string or of any type? Could you give me a
pointer to the explanation of this in the official doc ? >>
I don't know if this is the best solution, but it's nice and general:
remove-if: func [predicate blk args /local result keep-it][
result: make block! length? blk
repeat el blk [
; Have to use a temp variable here. "if" doesn't like "predicate :el
args"
; inline and putting it in parens breaks the evaluation.
keep-it: not predicate :el args
if keep-it [append/only result :el]
]
return result
]
>> remove-if :empty? ["" "A" "B" "" "C" "D" ""] none
== ["A" "B" "C" "D"]
--Gregg
[6/8] from: micael::gullmes::telenordia::se at: 13-Mar-2002 20:17
Hi,
here is a different approach:
>> foreach i t [if not equal? "" trim i [print i]]
one
two
This will also get rid of lines containing spaces.
hope it helps.
Brgds /Micael
-----Ursprungligt meddelande-----
Fran: Bansard Stephane [mailto:[p0665393--MAGELLAN--UMontreal--CA]]
Skickat: den 13 mars 2002 17:39
Till: [rebol-list--rebol--com]
Amne: [REBOL] getting rid of empty string in a block.
Hi all,
I want to sort a file by line alphabetic order. It appears it works at once
as I
write an idea ! Rebol is great !
str: copy []
foreach l (sort read/lines %my-file) [append str join l "^/"]
But I haven't thought of blank line which are empty string in the sort
read/lines resulting block.
I try using the string length. But I don't understand the following
evaluations
:
>> t: ["" "" "one" "two"]
>> foreach i t [print length? i]
0
0
3
3
>> foreach i t [if length? i [print i]]
one
two
>> foreach i t [if length? i <> 0 [print i]]
** Script Error: length? expected series argument of type: series port tuple
struct
** Near: if length? i <> 0
>> foreach i t [if (length? to-string i <> 0) [print i]]
one
two
----
What would be the best solution to get rid of the empty strings ? (or of any
empty values of a block, be they string or of any type? Could you give me a
pointer to the explanation of this in the official doc ?
Many thanks,
Best regards
Stephane
[7/8] from: p0665393:magellan:umontreal:ca at: 13-Mar-2002 13:10
Hi Patrick,
Thank you for your answer. I had forgotten the precedence rule! Sorry.
It's why
>> foreach i t [if (length? i <> 0) [print i]]
** Script Error: length? expected series argument of type: series port tuple
struct
** Near: length? i <> 0
wasn't working.
Best regards,
Stephane
[8/8] from: lmecir:mbox:vol:cz at: 15-Mar-2002 18:57
Hi Stephane,
the simplest way (AFAIK) is to use:
exclude ["1" "2" ""] [""]
Cheers
L