# How to change stdin separator (perl vs ruby)

**URL:** <https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246>\
**Category:** Ruby\
**Created:** [February 10, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246 "2010-02-10T16:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/1 "2010-02-10T16:22:07Z")

</div>

I have to process very very big text files and not line per line, I have  
to access to a group of lines seperated for a “%”. Then I tryed to  
access line per line and detect the separator for each line saving the  
lines in a buffer, obviously it works but it’s very slow (text files are  
bigger than 1GB).

Perl have an awesome feature that is to change the separator for  
iterating files (by default in all languages is “\n”), this simple and  
extremely efficent code do what I need:

$/ = “%”;  
while ($article = ) {  
#Iteration  
}

I can write a equivalent code in ruby but not (by far) as efficient as  
perl. Someone knows how can I change the separator for iterate the stdin  
in ruby?

Thanks!

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/2 "2010-02-10T16:42:12Z")

</div>

2010/2/10 Sak … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> $/ = “%”;  
> while ($article = ) {  
> #Iteration  
> }
> 
> I can write a equivalent code in ruby but not (by far) as efficient as  
> perl. Someone knows how can I change the separator for iterate the stdin  
> in ruby?

```
STDIN.each_line("%") { |line|
  # iteration
}

```

Stefan

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/3 "2010-02-10T16:42:47Z")

</div>

On Wed, Feb 10, 2010 at 4:23 PM, Sak … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can write a equivalent code in ruby but not (by far) as efficient as  
> perl. Someone knows how can I change the separator for iterate the stdin  
> in ruby?

Ruby has also $/ among its global variables. The methods #gets and  
#readline take it into account. Those methods and #each also accept a  
separator as an optional argument.

In Ruby 1.9 they additionally accept an integer, that’s the same as  
setting $/ to a reference to an integer in Perl.

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 4:46pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/4 "2010-02-10T16:46:53Z")

</div>

On Thu, Feb 11, 2010 at 12:23:55AM +0900, Sak … wrote:

> $/ = “%”;  
> while ($article = ) {  
> #Iteration  
> }

If your delimiter only occurs as a delimiter, and not in the data as  
well:

File.open(path\_to\_file, ‘r’) do |f|  
f.each(’%’) do |record|  
# Do stuff.  
end  
end

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/5 "2010-02-10T17:19:01Z")

</div>

Stefan L. wrote:

> 2010/2/10 Sak … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > $/ = “%”;  
> > while ($article = ) {  
> > ï¿½#Iteration  
> > }
> > 
> > I can write a equivalent code in ruby but not (by far) as efficient as  
> > perl. Someone knows how can I change the separator for iterate the stdin  
> > in ruby?
> 
> ```
> STDIN.each_line("%") { |line|
> # iteration
> }
> 
> ```
> 
> Stefan

Thanks Stefan for the answer, that’s exactly what I need, thanks Xavier  
explanation is perfect. Thanks Daniel, good observation.

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [February 10, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/how-to-change-stdin-separator-perl-vs-ruby/183246/6 "2010-02-10T17:24:56Z")

</div>

2010/2/10 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > $/ = “%”;  
> > while ($article = ) {  
> > #Iteration  
> > }
> 
> If your delimiter only occurs as a delimiter, and not in the data as well:

Note that this statement is true for \n as well!

> File.open(path\_to\_file, ‘r’) do |f|  
> f.each(‘%’) do |record|
> 
> # Do stuff.
> 
> end  
> end

You can have it simpler:

File.foreach path, ‘%’ do |record|

# so something with record

end

Kind regards

robert
