# Havin problem with set the flag in loop

**URL:** <https://www.ruby-forum.com/t/havin-problem-with-set-the-flag-in-loop/117358>\
**Category:** Rails\
**Created:** [October 16, 2007, 8:56am UTC](https://www.ruby-forum.com/t/havin-problem-with-set-the-flag-in-loop/117358 "2007-10-16T08:56:52Z")\
**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 16, 2007, 8:56am UTC](https://www.ruby-forum.com/t/havin-problem-with-set-the-flag-in-loop/117358/1 "2007-10-16T08:56:52Z")

</div>

Hi all,

I am having problem with array while setting the flag,

actaully when i get the array size should be all 44 other wise it should  
come to flag 0, now my array counts are 42 42 44, so it shows the flag =  
0, but for me it showing flag =1 , i think i have some loop problem,  
please let me know why?

@split\_record.each do |v|  
@split\_data=v.split("\*")  
@get\_size=@split\_data.size // after split my array is  
42 42 44  
if @get\_size == 44  
@flag=1  
else  
@flag=0  
end  
end  
return @flag

In this i am checking 3 records, i should get flag=1 if all are 44,  
but at present not matching, so it should shows floag=0, but for its  
showing flag =1, please let me know the problem, thanks in advance

---

<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 16, 2007, 11:51am UTC](https://www.ruby-forum.com/t/havin-problem-with-set-the-flag-in-loop/117358/2 "2007-10-16T11:51:17Z")

</div>

You should seriously consider reading  
[http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)

And this question is suited for ruby-lang

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

> @split\_record.each do |v|
> 
> ## In this i am checking 3 records, i should get flag=1 if all are 44, but at present not matching, so it should shows floag=0, but for its showing flag =1, please let me know the problem, thanks in advance
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<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 16, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/havin-problem-with-set-the-flag-in-loop/117358/3 "2007-10-16T12:49:51Z")

</div>

Vidya V. wrote:

> Hi all,
> 
> @split\_record.each do |v|  
> @split\_data=v.split(“\*”)  
> @get\_size=@split\_data.size // after split my array is  
> 42 42 44  
> if @get\_size == 44  
> @flag=1  
> else  
> @flag=0  
> end  
> end  
> return @flag

1. just from the code here it seems there is no need for u to use global  
variables (why not use local variables? better for mem usage). →   
instead of using  
@get\_size  
why not just use  
get\_size

2. the reason, simply, that the @flag=1 has to do with the fact that in  
the last iteration (=loop) the flag is set to one because the last var  
in the array is  
44, so if u check the value of the flag per loop you’ll get:

(begin loop)  
loop 1: @flag=0  
loop 2: @flag=0  
loop 3: @flag=1  
(end loop)

at the end of the loop, the variable @flag equals 1.  
AND:

> Posted by Pratik Naik (pratik)  
> on 16.10.2007 11:51  
> (Received via mailing list)
> 
> You should seriously consider reading  
> [http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)
> 
> And this question is suited for ruby-lang (forum#4)

enjoy.
