Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: none? ???

From: joel:neely:fedex at: 19-Apr-2002 8:10

Hi, again, Richard, I forgot one other performance issue I intended to raise: Richard Coffre wrote:
> tcs: [ > [ tcgrp01 > "05" "06" "07" "15" "16" "17" "25" "26" "27" "35" "36" "37" > ] > [ tcgrp02 "02" ] > ] > > texte: read/lines output > offset: 7 > > foreach ligne texte [ > foreach grp tcs [ > tc: remove/part copy/part ligne (offset + 2) offset > if not none? [ select grp tc ] [ > print rejoin [ tc "-" first grp ] > ] > ] > ] >
There's a lot of overhead involved in your two-level looping (both in time and in code complexity) that can be eliminated with a little pre-processing of your search data structure. It's easy to write a helper function that transforms your TCS into a simple block of key+value pairs... tcs: [ [ tcgrp01 "05" "06" "07" "15" "16" "17" "25" "26" "27" "35" "36" "37" ] [ tcgrp02 "02" ] ] unfold: func [groups /local result val] [ result: copy [] foreach group groups [ val: first group foreach key next group [ either found? find result key [ print rejoin ["Error: '" key "' already used!"] ][ insert insert tail result key val ] ] ] result ] The error detection is for free... ;-) The UNFOLD function walks across the entire group structure, but only once. The overhead of doing so is amortized across all of the input records, that now can use the native SELECT (which will be much faster than repeating the traversal in interpreted code for every input line). Now the work of processing your input data is much simpler: text: parse/all {100002 050042011006470 100002 050100000000000 100003 150042011006470 100004 250042011003653 100005 350042011004029 100006 450049750304500 100007 550042011006470 100007 020100000000000 100008 650042011003653 100009 750042011004029 100010 850049750304500 100011 060042011006470} "^/" tcs-pairs: unfold tcs inset: 8 foreach line text [ use [tc val] [ tc: copy/part at line inset 2 if found? val: select tcs-pairs tc [ print rejoin [ tc "-" val ] ] ] ] We get the following results: 05-tcgrp01 05-tcgrp01 15-tcgrp01 25-tcgrp01 35-tcgrp01 02-tcgrp02 06-tcgrp01 (Note that I changed some of the data to verify that both groups were present, and that the "do nothing if not found" rule would be followed.) -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]