# Stripping columns / puts'ing columns

**URL:** <https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195>\
**Category:** Ruby\
**Created:** [July 12, 2007, 10:38am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195 "2007-07-12T10:38:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 10:38am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/1 "2007-07-12T10:38:52Z")

</div>

Hi again,

still on my quest to learn ruby 😉 And yet another question - more a  
general “how-to-approach” thing, though.

Let’s say I have a text file with a number of rows (it’s a molecular  
sequence alignment, so each row = one sequence).  
I am only interested in those columns in which a certain character  
appears - no matter whether in all row or only one. The whole column  
should then be puts’ed.

After thinking about it for a moment, my idea was to store each row in  
an array, check each array position for the presence of that particular  
character and, if it is, found, to puts this array field [n] and the  
corresponding field from all other arrays. Now, there are a couple of  
potential problems such as hitting the same column multiple times  
because I search multiple arrays… But anyways, since I am rather new  
to programming and ruby in particular, that will take me a while - so I  
was wondering if there is a more elegant/efficient approach to this,  
before I waste my time on this only to find out it doesnt work or could  
be done more efficiently (wasted quite some time on another program the  
other day only to find out that it can be done with grep…yuck).

/Marc

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 10:56am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/2 "2007-07-12T10:56:42Z")

</div>

On 7/12/07, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> corresponding field from all other arrays. Now, there are a couple of  
> potential problems such as hitting the same column multiple times  
> because I search multiple arrays… But anyways, since I am rather new  
> to programming and ruby in particular, that will take me a while - so I  
> was wondering if there is a more elegant/efficient approach to this,  
> before I waste my time on this only to find out it doesnt work or could  
> be done more efficiently (wasted quite some time on another program the  
> other day only to find out that it can be done with grep…yuck).
> 
> /Marc

Does this do what you want?

arr = [[1,7,3,4],[5,6,7,8],[9,7,3,4]]  
new\_arr = arr.transpose

p new\_arr.select {|x| x.include?(7)}

Harry

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 11:04am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/3 "2007-07-12T11:04:42Z")

</div>

Alle giovedÃ¬ 12 luglio 2007, Marc H. ha scritto:

> other day only to find out that it can be done with grep…yuck).
> 
> /Marc

If I understand your problem correctly, you can try this:

max\_length = rows.map{|r| r.length}.max  
cols = rows.map{|r| r.split(’’) + Array.new(max\_length - r.length)  
}.transpose  
interesting\_cols = cols.select{ |c| c.include?(interesting\_character) }

Your idea is correct: transform each row from a string to an array of  
characters. So, youave a nested array, with column elements inside rows.  
Since you need to select character basing on columns, we need to  
transpose  
the outer array. This will leave you with a nested array where each  
element  
of the outer array is a column, on which you can use select. The only  
problem  
is that to use this approach, your rows need to be of the same length.  
If  
they aren’t, then you need to add to the array obtained split another  
array,  
made of empty strings, whose size is the difference between the length  
of the  
longest line and the length of the line. At the end, you may need to  
remove  
those elements (you can do this with

interesting\_cols.each{|c| c.delete(’’)}

). If all your lines have the same length, then you don’t need to do  
this and  
the first two lines of my code become

cols = rows.map{|r| r.split(’’)}.transpose

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 11:06am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/4 "2007-07-12T11:06:48Z")

</div>

Stefano C. wrote:

> I hope this helps
> 
> Stefano

Hi and thanks a lot - yes I guess that helps. The rows are of equal  
length, so that wont be an issue.

Cheers,  
Marc

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 11:11am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/5 "2007-07-12T11:11:43Z")

</div>

> Does this do what you want?
> 
> arr = [[1,7,3,4],[5,6,7,8],[9,7,3,4]]  
> new\_arr = arr.transpose
> 
> p new\_arr.select {|x| x.include?(7)}

The rows need to have the same number of elements.

Harry

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 11:31am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/6 "2007-07-12T11:31:45Z")

</div>

2007/7/12, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> other day only to find out that it can be done with grep…yuck).  
> I’m missing one crucial bit of information: can your files grow so  
> large that they do not fit into memory? If that’s the case you need a  
> two or more phased approach.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [July 12, 2007, 11:47am UTC](https://www.ruby-forum.com/t/stripping-columns-putsing-columns/109195/7 "2007-07-12T11:47:59Z")

</div>

Robert K. wrote:

> 2007/7/12, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > other day only to find out that it can be done with grep…yuck).  
> > I’m missing one crucial bit of information: can your files grow so  
> > large that they do not fit into memory? If that’s the case you need a  
> > two or more phased approach.
> 
> Kind regards
> 
> robert

I am using single protein alignments, so that any single row should  
never exceed ~ 2000 characters (or in other word my sequences are  
rarely longer than a couple hundred characters), with 20 rows tops. So  
that should not be an issue, I guess.
