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 7:37

Hi, Richard, Richard Coffre wrote:
> Hi, > > I got an input file like this : > > 100002 050042011006470 > 100002 050100000000000 > 100003 050042011006470 > 100004 050042011003653 > 100005 050042011004029 > 100006 050049750304500 > 100007 250042011006470 > 100007 250100000000000 > 100008 250042011003653 > 100009 250042011004029 > 100010 250049750304500 > 100011 060042011006470 > > and the following code : > > 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 ; Keep the two first characters after the index and the extra space > if not none? [ select grp tc ] [ > print rejoin [ tc "-" first grp ] > ] > ] > ] > > In order to test if tc is included in the block. If so, it should print the first element of the sub block. > > Currently, it always prints the first element. Why ? >
There are at least three major flaws in the above code: 1) You are handing NONE? a *block* when you write if not none? [ select grp tc ] I suspect you confused square brackets with parentheses, but no punctuation is needed at all. Since any block value (no matter what contents) differs from NONE? , the test NOT NONE? [...] always succeeds. 2) You have no exit from the inner loop after success, so the subsequent groups within TCS will be tested even if after finding the appropriate value (and -- based on the previous comment -- you'll get bogus output from subsequent tests). 3) You are using SELECT instead of FIND. That will give you an error if you are searching for e.g. "37" because SELECT returns the value *following* the search target (and there's nothing after "37", so you'll get NONE). There are also the minor inefficiencies that: 1) You are extracting the tested substring on every pass through the inner loop, even though it shouldn't change. 2) You are copying and then discarding the prefix instead of just copying the part you need. And a semantic confusion that OFFSET isn't actually the offset into the string of the part of interest, it is the amount to discard. If you're discarding 7 characters, then the portion of interest begins at position 8 (thanks to the 1-origin mess). Fixing those problems, and simulating your input by parsing the sample string... text: parse/all {100002 050042011006470 100002 050100000000000 100003 050042011006470 100004 050042011003653 100005 050042011004029 100006 050049750304500 100007 250042011006470 100007 250100000000000 100008 250042011003653 100009 250042011004029 100010 250049750304500 100011 060042011006470} "^/" tcs: [ [ tcgrp01 "05" "06" "07" "15" "16" "17" "25" "26" "27" "35" "36" "37" ][ tcgrp02 "02" ] ] inset: 8 foreach line text [ use [tc] [ tc: copy/part at line inset 2 foreach grp tcs [ if found? find grp tc [ print rejoin [ tc "-" first grp ] break ] ] ] ] We get this output: 05-tcgrp01 05-tcgrp01 05-tcgrp01 05-tcgrp01 05-tcgrp01 05-tcgrp01 25-tcgrp01 25-tcgrp01 25-tcgrp01 25-tcgrp01 25-tcgrp01 06-tcgrp01 Note that the sample data you supplied all fall within the first group, however... text: parse/all {100002 050042011006470 100002 010100000000000 100003 020042011006470 100004 030042011003653 100005 040042011004029 100006 050049750304500 100007 060042011006470 100007 070100000000000 100008 080042011003653 100009 090042011004029 100010 100049750304500 100011 110042011006470} "^/" is a test data set that exercises both data groups and also your do nothing if not found case: 05-tcgrp01 02-tcgrp02 05-tcgrp01 06-tcgrp01 07-tcgrp01 Hope this helps! -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 ]