How to change stdin separator (perl vs ruby)

S

Sak ..

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 = <STDIN>) {
#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!
 
S

Stefan Lang

2010/2/10 Sak .. said:
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:

$/ =3D "%";
while ($article =3D <STDIN>) {
=A0#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
 
X

Xavier Noria

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.
 
D

Daniel Bye

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 = <STDIN>) {
#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
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!
--
Posted via http://www.ruby-forum.com/.


!DSPAM:4b72d13c186061354869328!

--
Daniel Bye
_
ASCII ribbon campaign ( )
- against HTML, vCards and X
- proprietary attachments in e-mail / \
 
R

Robert Klemme

2010/2/10 Daniel Bye said:
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|
=A0f.each('%') do |record|
=A0 =A0# Do stuff.
=A0end
end

You can have it simpler:

File.foreach path, '%' do |record|
# so something with record
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Sak ..

Stefan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top