Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Using block index as variable suffix
Pages: [1] Print
Author Topic: Using block index as variable suffix  (Read 271 times)
Stuart
Guest


Email
Using block index as variable suffix
« on: November 04, 2004, 08:53:17 PM »

I have a block like this

block: ["abc" "def" "ghi"]

The actual text of the block will contain text I want o search for in a file.

So later I am trying to search for those block elements one after the other as follows having read a file into "lines":

foreach line lines [
         if find line pick block 1 [count1: count1 + 1]
         if find line pick block 2 [count2: count2 + 1]
         if find line pick block 3 [count3: count3 + 1]
]

Instead of having to type in each line seperately is there a way I can do a foreach on the elements of block and then use the index number to append to the 'count' variable so it is all nicely packaged in one line?

Thanks for the assistance.

Stuart
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
Using block index as variable suffix
« Reply #1 on: November 05, 2004, 12:52:15 AM »

Something like this should do the job...
Code:
REBOL []

block: ["abc" "def" "ghi"]
counts: array/initial length? block 0

lines: [
  "aaa def bbb abc"
  "sss ghi aaa abc ghi"
  "fff ddd"
  "ghi abc def abc"
]

foreach line lines [
    repeat num length? block [
       ln: line
       while [not none? ln][
           if ln: find/tail ln block/:num [poke counts num counts/:num + 1]
       ]
   ]
]
probe counts
Looking at your method, I don't think it'd handle cases where the same text is repeated more than once in a line.  This is why I have the search in a WHILE loop, it continuing the search if the text is found.

Hope this helps.
Logged

- Carl Read
Stuart
Guest


Email
Using block index as variable suffix
« Reply #2 on: November 05, 2004, 11:42:32 AM »

Thanks Carl, that is a great help.

Stuart
Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Using block index as variable suffix
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 12, 2008, 12:23:41 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2251 Posts in 589 Topics by 2420 Members
Latest Member: pharmacytovvv

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc