# Parse the data in an array index in ruby

**URL:** <https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410>\
**Category:** Ruby\
**Created:** [April 6, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410 "2010-04-06T18:32:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 6, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/1 "2010-04-06T18:32:37Z")

</div>

Hi,

I have a array in a class. And i want to parse the data in the array’s  
index.  
I tried split method but it said private method ‘split’…

For example

class AA

```
@data...
........

```

end  
@data[0]=" ABC HGD KKK 111…" sth like that i need the datas seperately  
like ABC  
How can i do this?

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 6, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/2 "2010-04-06T18:51:33Z")

</div>

I don’t know what exactly is in @data so I may be wrong, but you could  
try something like this :  
class MyClass  
def initialize(data)  
@data = data  
end

def split(regexp = /\s+/)  
@data.collect { |e| e.split(regexp) }  
end  
end

arr = [‘ABC DEF GHI J KLMN OPQRS’, ‘TUV WX Y Z’]  
c = MyClass.new(arr)  
c.split

# returns : [[“ABC”, “DEF”, “GHI”, “J”, “KLMN”, “OPQRS”], [“TUV”, “WX”,

“Y”, “Z”]]

Le 06/04/2010 18:32, Brk A. a Ã©crit :

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 6, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/3 "2010-04-06T20:56:49Z")

</div>

“Brk A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> ## @data… … end @data[0]=" ABC HGD KKK 111…" sth like that i need the datas seperately like ABC How can i do this?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Try the folloving snippet:  
@data = []  
@data[0] = “ABC HGD KKK 111”  
m = @data[0].split(’ ')  
puts m.inspect.to\_s # =\> [“ABC”, “HGD”, “KKK”, “111”]  
hth gfb

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 7, 2010, 8:50am UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/4 "2010-04-07T08:50:05Z")

</div>

Actually i try to fill a table

class Sorm\_Table

def initialize # define table structure and array for data  
@template =  
Struct.new(:nofcall,:obj,:rpn,:rac,:cg1,:cgpn,:cd1,:cdpn,:stt)  
@data=Array.new(1000){Array.new()}  
@obj=[‘10000’,‘10016’]  
end # initialize

def add\_row(\_nofcall,\_obj, \_rpn,\_rac,\_cg1,\_cgpn,\_cd1,\_cdpn,\_stt)  
s=@template.new(\_nofcall,\_obj, \_rpn,\_rac,\_cg1,\_cgpn,\_cd1,\_cdpn,\_stt)  
@data[$indx] \<\< s  
$indx=$indx+1  
end

after i call add row func  
tbl.add\_row($callno,tbl.obj[$index2],final\_data[Enum::PN2],final\_data[Enum::AC2],final\_data[Enum::PN1],final\_data[Enum::AC1],final\_data[Enum::PN2],final\_data[Enum::AC2],‘nACT’)

i try to add these datas like @data[0][0]=$callno  
@data[0][1]=tbl.obj[$index2]  
so i can reach the $callno of the fist row of the table easily but now  
@data[0] carries all of the data i can not seperate. Cause of this i am  
trying to split.  
How can i do it?

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 6, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/5 "2010-04-06T22:09:00Z")

</div>

Brk A. wrote:

> Hi,
> 
> I have a array in a class. And i want to parse the data in the array’s  
> index.  
> I tried split method but it said private method ‘split’…

Best to show your code, so we don’t have to guess.

The error suggests you did something like:

foo = bar.split

where bar is not a String. Unfortunately, all objects inherit  
Kernel#split, which is only there for legacy reasons (a nasty perlism  
which splits $\_)

But you should treat the ‘private method’ error as meaning ‘no such  
method’

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 7, 2010, 9:38am UTC](https://www.ruby-forum.com/t/parse-the-data-in-an-array-index-in-ruby/186410/6 "2010-04-07T09:38:58Z")

</div>

2010/4/7 Brk A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Actually i try to fill a table
> 
> class Sorm\_Table
> 
> def initialize # define table structure and array for data  
> @template =  
> Struct.new(:nofcall,:obj,:rpn,:rac,:cg1,:cgpn,:cd1,:cdpn,:stt)

There is no point in recreating this class over and over again. Rather  
do

class Sorm\_Table  
Template =  
Struct.new(:nofcall,:obj,:rpn,:rac,:cg1,:cgpn,:cd1,:cdpn,:stt)

> @data=Array.new(1000){Array.new()}

There is an inconsistency in the way you fill @data: you initialize it  
with a certain size and stuff Array instances in there and later you  
stuff a single @data (or Template with my modification) instances in  
every nested Array. At the moment your nested Arrays look completely  
superfluous.

Note also that pre allocation of an Array is not necessary in Ruby -  
Arrays can grow.

> @obj=[‘10000’,‘10016’]

No idea what that is used for. As far as I can see there is no usage  
of this variable. The fact though that you put strings in there that  
look like integers is suspicious. If these are to be treated as ints  
then make them ints.

> end # initialize
> 
> def add\_row(\_nofcall,\_obj, \_rpn,\_rac,\_cg1,\_cgpn,\_cd1,\_cdpn,\_stt)  
> s=@template.new(\_nofcall,\_obj, \_rpn,\_rac,\_cg1,\_cgpn,\_cd1,\_cdpn,\_stt)  
> @data[$indx] \<\< s  
> $indx=$indx+1  
> end

Modifying a global variable from inside a class instance is a very bad  
idea. Remember that you define a class in order to have _multiple_  
instances of it. So your $indx should rather be @indx but more likely  
it’s completely superfluous. You can simply append to the array.  
Your #add\_row could then look like this:

def add\_row(\*args)  
@data \<\< Template.new(\*args)  
self  
end

> after i call add row func  
> tbl.add\_row($callno,tbl.obj[$index2],final\_data[Enum::PN2],final\_data[Enum::AC2],final\_data[Enum::PN1],final\_data[Enum::AC1],final\_data[Enum::PN2],final\_data[Enum::AC2],‘nACT’)
> 
> i try to add these datas like @data[0][0]=$callno  
> @data[0][1]=tbl.obj[$index2]

A global variable name with a number in there looks suspicious as  
well. It is most likely that you want to store the index in  
Sorm\_Table instances or even just use @data.size.

> so i can reach the $callno of the fist row of the table easily but now  
> @data[0] carries all of the data i can not seperate. Cause of this i am  
> trying to split.  
> How can i do it?

Frankly, the logic of your application is not fully clear to me. It  
seems though that some more changes are in order. Can you provide a  
description of what you want to do? You should at least provide an  
explanation what $callno and all the other variables mean and what  
types you stuff in there.

Kind regards

robert
