How to pad variable-length vectors with NULLs to a fixed length?

19 views Asked by At

I have a set of variable-length integer vectors and I want to align them by inserting NULL values at the end to make them have the same fiexed length. How can I achieve this? For example, I have a tuple “a“ containing variable-length vectors [1..3], [1..5], and [1..6]. How can I transform them into vectors of length 6?

col1    col2    col3
1        1        1
2        2        2
3        3        3
null     4        4
null     5        5
null     null     6
1

There are 1 answers

0
Shena On BEST ANSWER

Method 1: Use append!:

a=[1..3,1..5,1..6]
def f(mutable x, N): x.copy().append!(array(DOUBLE,N - size(x),N - size(x),NULL))
each(f{,6},a)

Output:

col1    col2    col3
 1       1       1
 2       2       2
 3       3       3
         4       4
         5       5
                 6

Method 2: Add 6 NULL values to each of these vectors to ensure their length is greater than or equal to 6. Then extract the first six elements of each vector:

a = [1..3, 1..5, 1..6]
def fillNull(x,n): take(x<-take(00i, n), n)
each(fillNull{, 6}, a)