# Data count problem in array

**URL:** <https://www.ruby-forum.com/t/data-count-problem-in-array/116859>\
**Category:** Ruby\
**Created:** [October 10, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/data-count-problem-in-array/116859 "2007-10-10T14:43:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 10, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/data-count-problem-in-array/116859/1 "2007-10-10T14:43:33Z")

</div>

hi all,

now i am facing problem with count, actually i need to count the data  
after spliting like this is my one data

@aa=LIN_EDIA000005_000000570_570_0697_RC_SHEPLER’S EXCLU_100_SHEPLER’S  
EXCLUSIVE_6042142403_SHEPLERS COGNAC HORNBACK CAIMAN  
TAIL_20070719_20070719 **02_724178914197_** _**070  
A**_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*50384~

- i need to split based on \*,if you split base on \*, we can get number  
count 44, but in coding i don’t know how to count in array, i user  
@a.size function, but its shows the count no 1 only, please help me  
to count this record and get the array index.

i have given my coding, what i tried in below,

```
         @a=@aa.split("~")
          @a.each do |v|
            @h=v.split("*")
             @test=@h.size
           end
           return @test

```

This is the sample records, this record will follows one by one at the  
end of ~.

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 10, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/data-count-problem-in-array/116859/2 "2007-10-10T14:54:50Z")

</div>

On 10/10/07, Vidya V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - i need to split based on \*,if you split base on \*, we can get number  
> end  
> return @test
> 
> This is the sample records, this record will follows one by one at the  
> end of ~.

The problem here is that \* is a special character for regular  
expressions, so you will have to escape it. This works:

@a=@aa.split(“~”)  
@a.each do |v|  
@h=v.split(“\*”)  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 12, 2007, 6:26am UTC](https://www.ruby-forum.com/t/data-count-problem-in-array/116859/3 "2007-10-12T06:26:07Z")

</div>

On Oct 10, 8:54 am, “Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > i have given my coding, what i tried in below,
> 
> Jesus.- Hide quoted text -
> 
> - Show quoted text -

the @ character should not be used unless the variable is an instance  
variable (@x) or class variable (@@x)

you’re showing your perl background

the character string must be enclosed within parentheses

aa=“LIN_EDIA000005_000000570_570_0697_RC_SHEPLER’S EXCLU_100_SHEPLER’S  
\ # '' instructs ruby to ignore the carriage return  
EXCLUSIVE_6042142403_SHEPLERS COGNAC HORNBACK CAIMAN  
TAIL_20070719_20070719 **02_724178914197_** _**070  
A**_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*50384~”

puts “aa is a #{aa.class} of length #{aa.size}”  
ab = aa.split(‘~’)  
puts “ab is #{ab.class} of size #{ab.size}”

ab.each do |a|  
puts “a is a #{a.class} of length #{a.size}”  
b = a.split(‘\*’) # no need to escape unless you use a regex  
split(/\*/)  
puts “b is a #{b.class} of size #{b.size}”  
end

aa is a String of length 195  
ab is Array of size 1  
a is a String of length 194  
b is a Array of size 44 b,size or b.length  
gives the size of the array  
Complete(0)
