# \[File -\> Lines -\> Chars\] Logic issue (open)

**URL:** <https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022>\
**Category:** Ruby\
**Created:** [November 15, 2006, 10:38am UTC](https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022 "2006-11-15T10:38:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 10:38am UTC](https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022/1 "2006-11-15T10:38:21Z")

</div>

Hi there

Just a simple question from a newbie =)

I have a file that can have N lines. Those lines are string of  
characters, and each character is a sign of a combination i have to  
store, but at the same time i have to be able to treat each sign of each  
combination separately, in order to compare them and all.

For instance:

# Combination file

11XX22X1XX12X23  
XX12XX122X212X2  
XX2X11XX11XX111

My program has to return and array for each line, and that array should  
have stored in it each sign of the line.

My question is…how can i Read EACH line of the file, explode each char  
of the array and store it?

Thx.

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 10:49am UTC](https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022/2 "2006-11-15T10:49:30Z")

</div>

Flaab M. wrote:

> of the array and store it?
> 
> Thx.

ary = File.open(file).readlines.map {|l|  
l.chomp.split //  
}

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022/3 "2006-11-15T10:51:32Z")

</div>

Flaab M. wrote:

> of the array and store it?  
> The program below reads lines and creates an array of arrays (a  
> two-dimensional array), each inner cell contains a single character:

* * *

#!/usr/bin/ruby -w

data = “11XX22X1XX12X23\nXX12XX122X212X2\nXX2X11XX11XX111\n”

lines = []

data.each do |line|  
lines \<\< line.split("")  
end

p lines

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 10:53am UTC](https://www.ruby-forum.com/t/file-lines-chars-logic-issue-open/76022/4 "2006-11-15T10:53:32Z")

</div>

On 15/11/06, Flaab M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> of the array and store it?
> 
> Thx.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You can use each\_byte to iterate over every character of a string.  
Just be aware that it will pass an int character code, not the actual  
character itself, into the associated block but you can convert it  
back using the chr method like so:  
irb(main):026:0\> “hello”.each\_byte{|c|puts c.chr}  
h  
e  
l  
l  
o

Farrel
