Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Checking File for a line

Reply
Thread Tools

Checking File for a line

 
 
Peter Loftus
Guest
Posts: n/a
 
      11-23-2007
Need to check a file for a line
Lets say "this is an example"

I am going to just take every line into an array
and then have a For loop to check the array for that line.

Im pretty sure this will work but is there any cleaner way to do it?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Lee Jarvis
Guest
Posts: n/a
 
      11-23-2007
Peter Loftus wrote:
> Im pretty sure this will work but is there any cleaner way to do it?


File.readlines('file.txt.).each do |line|
puts 'got it!' if line =~ /regexp/
end

Hope the helps.

Regards,
Lee
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-23-2007
2007/11/23, Lee Jarvis <(E-Mail Removed)>:
> Peter Loftus wrote:
> > Im pretty sure this will work but is there any cleaner way to do it?

>
> File.readlines('file.txt.).each do |line|
> puts 'got it!' if line =~ /regexp/
> end


This is more efficient - no need to load the whole file into mem to
find a single line:

File.foreach "file.txt" do |line|
if /rx/ =~ line
puts "found it"
break
end
end

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Dejan Dimic
Guest
Posts: n/a
 
      11-23-2007
On Nov 23, 1:08 pm, Robert Klemme <(E-Mail Removed)> wrote:
> 2007/11/23, Lee Jarvis <(E-Mail Removed)>:
>
> > Peter Loftus wrote:
> > > Im pretty sure this will work but is there any cleaner way to do it?

>
> > File.readlines('file.txt.).each do |line|
> > puts 'got it!' if line =~ /regexp/
> > end

>
> This is more efficient - no need to load the whole file into mem to
> find a single line:
>
> File.foreach "file.txt" do |line|
> if /rx/ =~ line
> puts "found it"
> break
> end
> end
>
> Cheers
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end


Check out

ri IO:foreach
ri IO#each

and use what more suits your needs.

Dima
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-23-2007
2007/11/23, Dejan Dimic <(E-Mail Removed)>:
> On Nov 23, 1:08 pm, Robert Klemme <(E-Mail Removed)> wrote:
> > 2007/11/23, Lee Jarvis <(E-Mail Removed)>:
> >
> > > Peter Loftus wrote:
> > > > Im pretty sure this will work but is there any cleaner way to do it?

> >
> > > File.readlines('file.txt.).each do |line|
> > > puts 'got it!' if line =~ /regexp/
> > > end

> >
> > This is more efficient - no need to load the whole file into mem to
> > find a single line:
> >
> > File.foreach "file.txt" do |line|
> > if /rx/ =~ line
> > puts "found it"
> > break
> > end
> > end
> >
> > Cheers
> >
> > robert
> >
> > --
> > use.inject do |as, often| as.you_can - without end

>
> Check out
>
> ri IO:foreach
> ri IO#each
>
> and use what more suits your needs.


I am not sure I understand this posting of yours. Are you talking to
the OP or to me? If you are talking to me, foreach is what I used in
the example above.

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Lee Jarvis
Guest
Posts: n/a
 
      11-23-2007
> This is more efficient - no need to load the whole file into mem to
> find a single line:
>
> File.foreach "file.txt" do |line|
> if /rx/ =~ line
> puts "found it"
> break
> end
> end
>
> Cheers
>
> robert


Ahh, good point, touche my good man.

Regards,
Lee

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-24-2007
2007/11/23, Lee Jarvis <(E-Mail Removed)>:
> > This is more efficient - no need to load the whole file into mem to
> > find a single line:
> >
> > File.foreach "file.txt" do |line|
> > if /rx/ =~ line
> > puts "found it"
> > break
> > end
> > end
> >
> > Cheers
> >
> > robert

>
> Ahh, good point, touche my good man.


While we're at it: there is another solution - probably a bit more elegant:

require 'enumerator'
File.to_enum(:foreach,"file.txt").grep(/rx/) {|li| puts li; break}



Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Dejan Dimic
Guest
Posts: n/a
 
      11-25-2007
On Nov 24, 8:58 am, Robert Klemme <(E-Mail Removed)> wrote:
> 2007/11/23, Lee Jarvis <(E-Mail Removed)>:
>
> > > This is more efficient - no need to load the whole file into mem to
> > > find a single line:

>
> > > File.foreach "file.txt" do |line|
> > > if /rx/ =~ line
> > > puts "found it"
> > > break
> > > end
> > > end

>
> > > Cheers

>
> > > robert

>
> > Ahh, good point, touche my good man.

>
> While we're at it: there is another solution - probably a bit more elegant:
>
> require 'enumerator'
> File.to_enum(:foreach,"file.txt").grep(/rx/) {|li| puts li; break}
>
>
>
> Kind regards
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end


That's Ruby beauty, express yourself in a way that suits you the most
and still we all can understand your dialect and enjoy.

Dima
 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      11-25-2007
Dejan Dimic wrote:
> On Nov 24, 8:58 am, Robert Klemme <(E-Mail Removed)> wrote:
>> > > end

>> File.to_enum(:foreach,"file.txt").grep(/rx/) {|li| puts li; break}
>>
>>
>>
>> Kind regards
>>
>> robert
>>
>> --
>> use.inject do |as, often| as.you_can - without end

>
> That's Ruby beauty, express yourself in a way that suits you the most
> and still we all can understand your dialect and enjoy.
>


Speak for yourself. I found that solution particularly distasteful. It
turned a simple, easy to read solution, into a convoluted one-liner
mess. And on my system the enumerator solution is 50% slower. But,
that seems to be the Ruby Way, so at some point I guess I'll have to
drink the koolaid and declare how beautiful solutions like that are.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
Reading in external file - error checking and line numbers... Hugh Macdonald Python 2 09-07-2005 09:58 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments