Split, combine ok but where's extract?

Alex Tweedly alex at tweedly.net
Sun Feb 13 19:27:40 EST 2005


David Vaughan wrote:

>
> Perhaps I have misunderstood the depth of the problem. I do not appear 
> to have the described difficulty with the example data.

The problem (as I understand it) was to split out the columns of the 
keys and/or the data of an array.

That's what the code you first proposed did (or tried to do); the 
fragment shown in this (tonight's) email is only the first half of the 
code - and therefore doesn't get far enough into it to demonstrate the 
problem.

> Here is code (assume a couple of testing fields, "f1" and "f2") and 
> the results using your actual example data.
>
>   put "1,2" into myArray[a,b]
>   put "1,3" into myarray[c,d]
>   put the keys of myArray into field "f1"
>   combine myArray with return
>   put myArray into field "f2"

Indeed, there's no problem there - it would be the next part which 
dissected the columns of the data that shows the problem.

Here's the complete section of code as you first sent it (with a couple 
of "with return"s added as you suggested)

> put the keys of myArray into tKeys
> split tKeys with return and comma
> put the keys of tKeys into v1
> combine tKeys  with return   -- discards the keys
> put tKeys into v2
>
> combine myArray  with return  -- discards the keys
> split myArray with return and comma
> put the keys of myArray into v3
> combine myArray  with return
> put myArray into v4
>
> Now, v1=a, v2=b, v3=n1, v4=n2
> (or close enough if I made a coding error)

i.e. v1 is the first column (of the keys), v2 is the second, v3 is the 
first column from the data and v4 is the second column from the data.

Now if you take my data
   put "1,2" into myArray[a,b]
  put "1,3" into myarray[c,d]

then run the above algorithm, what you get is
 v1 = a \r b
 v2 = c \r d
 v3 = 1
 v4 = 3

which is not what we need - the duplicate item "1" in column 3 (col 1 of 
the data) caused a problem.

> <snip>
> puts them back in an array. The only relevant possibility of 
> duplication appears to be with the keys of the original array, which 
> by definition were not duplicated anyway.
>
Not really.  By definition, the *complete* keys of the original array 
are not duplicated; but the problem arises because items within any 
particular column CAN be duplicates.   Here's your second code fragment 
- but with different values to demonstrate the problem

  put field "f1" into tData -- the a,b \r c,d data     -- LET's make f1 
contain   3,4 \r3,5
  put field "f2" into tKeys -- the 1,2 \r 1,3 data
  split tData with return                                       -- 
causes a problem
  put 1 into i
  repeat for each line x in tKeys
    put tData[i] into myArray[x]
    add 1 to i
  end repeat
-- and to prove it happened
  combine myArray with return and "#"
  put myArray into field "f2"

And this shows the problem. The 3rd line "split tData with return" 
results in a single array entry, tData[3] = 5, because of the 
duplication of the "3" - even though "3,4" didn't duplicate "3,5"

> I receive the digest so discussions may be slow.

In some ways this discussion is more complex than it need be - Xavier's 
second example (including the code he posted) was simply separating out 
the columns (and merging the columns) of a single columnar table.

So the problem is :  given a table
1,2,3,4
5,6,7,8
9,10,11,12

generate separate lists of the columns
v1 = 1 \r 5 \r 9     (spaces for readability only)
v2 = 2 \r 6 \r 10
v3 = 3 \r 7 \r 11
v4 = 4 \r 8 \r 12

This is (in effect) what your initial code proposal would do (adjusting 
for a table instead of an array), and is also what Xavier's code would do.

But given different input

1,2,3,4
5,2,7,8
9,10,11,12

his would give
v1 = 1 \r 5 \r 9     (spaces for readability only)
v2 = 2 \r 2 \r 10
v3 = 3 \r 7 \r 11
v4 = 4 \r 8 \r 12
and re-merging would get back the original input value.

However, your's would give
v1 = 1 \r 5 \r 9     (spaces for readability only)
v2 = 2 \r 10
v3 = 7 \r 11
v4 = 8 \r 12
and there would be no way to recover the initial value.

-- 
Alex Tweedly       http://www.tweedly.net

-------------- next part --------------
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.7 - Release Date: 10/02/2005


More information about the use-livecode mailing list