empty-line indexes and items removal - ruby

For general discussion related FlowStone
Post Reply
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

empty-line indexes and items removal - ruby

Post 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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: empty-line indexes and items removal - ruby

Post 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"]
"There lies the dog buried" (German saying translated literally)
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: empty-line indexes and items removal - ruby

Post 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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply