Page 1 of 1

empty-line indexes and items removal - ruby

Posted: Fri Jan 15, 2016 10:45 pm
by tester
Two arrays scenario.

array 1 contains some empty lines:

a1
b1
[empty line]
[empty line]
e1
f1

array 2 is full.

a2
b2
c2
d2
e2
f2

both arrays are identical in size.

in ruby:

1) How to get a list of indexes of empty lines in array 1?

2) How to use the list of (these) indexes, to remove corresponding lines in array 2?

p.s.: still on FS 3.8.

Re: empty-line indexes and items removal - ruby

Posted: Fri Jan 15, 2016 11:04 pm
by tulamide
There is no such thing as an empty line in an array. Every entry in an array is an object. But you could use the nil-object.

Then this is doing what you ask for:

Code: Select all

a1 = 1, 2, nil, nil, 5, 6
a2 = "a", "b", "c", "d", "e", "f"

a1.each.with_index {|v, i| if v == nil then a2[i] = nil end}

a2.compact!
#a2 is now ["a", "b", "e", "f"]

Re: empty-line indexes and items removal - ruby

Posted: Mon Jan 18, 2016 8:13 pm
by tester
Thanks, will check it.

BTW, nil or empty - depends on layer of reference. On GUI layer it's empty line, for programming language - it's nil.