Checking File for a line

Discussion in 'Ruby' started by Peter Loftus, Nov 23, 2007.

  1. Peter Loftus

    Peter Loftus Guest

    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/.
     
    Peter Loftus, Nov 23, 2007
    #1
    1. Advertising

  2. Peter Loftus

    Lee Jarvis Guest

    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/.
     
    Lee Jarvis, Nov 23, 2007
    #2
    1. Advertising

  3. 2007/11/23, Lee Jarvis <>:
    > 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
     
    Robert Klemme, Nov 23, 2007
    #3
  4. Peter Loftus

    Dejan Dimic Guest

    On Nov 23, 1:08 pm, Robert Klemme <> wrote:
    > 2007/11/23, Lee Jarvis <>:
    >
    > > 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
     
    Dejan Dimic, Nov 23, 2007
    #4
  5. 2007/11/23, Dejan Dimic <>:
    > On Nov 23, 1:08 pm, Robert Klemme <> wrote:
    > > 2007/11/23, Lee Jarvis <>:
    > >
    > > > 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
     
    Robert Klemme, Nov 23, 2007
    #5
  6. Peter Loftus

    Lee Jarvis Guest

    > 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/.
     
    Lee Jarvis, Nov 23, 2007
    #6
  7. 2007/11/23, Lee Jarvis <>:
    > > 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
     
    Robert Klemme, Nov 24, 2007
    #7
  8. Peter Loftus

    Dejan Dimic Guest

    On Nov 24, 8:58 am, Robert Klemme <> wrote:
    > 2007/11/23, Lee Jarvis <>:
    >
    > > > 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
     
    Dejan Dimic, Nov 25, 2007
    #8
  9. Peter Loftus

    7stud -- Guest

    Dejan Dimic wrote:
    > On Nov 24, 8:58 am, Robert Klemme <> 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/.
     
    7stud --, Nov 25, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,325
    Matt Humphrey
    Oct 18, 2004
  2. Hugh Macdonald
    Replies:
    2
    Views:
    295
    Hugh Macdonald
    Sep 7, 2005
  3. kaushikshome
    Replies:
    4
    Views:
    779
    kaushikshome
    Sep 10, 2006
  4. scad
    Replies:
    23
    Views:
    1,179
    Alf P. Steinbach
    May 17, 2009
  5. Marek Stepanek
    Replies:
    12
    Views:
    420
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page