[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

data count problem in array

Vidya Vidya

10/10/2007 12:44:00 PM

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 ~.
--
Posted via http://www.ruby-....

2 Answers

Jesús Gabriel y Galán

10/10/2007 12:54:00 PM

0

On 10/10/07, Vidya Vidya <vidhu1232001@yahoo.co.in> wrote:
> 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 ~.

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.

Bernard Kenik

10/12/2007 4:25:00 AM

0



On Oct 10, 8:54 am, "Jesús Gabriel y Galán" <jgabrielyga...@gmail.com>
wrote:
> On 10/10/07, Vidya Vidya <vidhu1232...@yahoo.co.in> wrote:
>
>
>
>
>
> > 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("*")
> > @te...@h.size
> > 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.- 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 CAIMANTAIL*20070719*20070719**02*724178914197******070A**********************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)