[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: patten match

Robert Dober

6/30/2007 11:36:00 AM

On 6/19/07, geetha <sangeetha.geethu05@gmail.com> wrote:
> Hi Good morning,
>
> I am having one text file. In this I need to remove all the new lines
> and I need to match the keyword what I am giving.
>
> Please any one help me....
>
> Thanks,
> S.Sangeetha.

Hmm removing the newlines can be done in different ways, I'll show you
one below.
However it is not clear to me what you mean by matching keywords, as a
sample application I will replace the keyword by _keyword_, but maybe
you shall ask again.

def remove_nl_and_kw file_name, key_word="ruby"
File.readlines(file_name).
map{|line| line.chomp}.
gsub(key_word, "_" << key_word << "_").
join

end

If you had the newlines removed because keywords might spwan lines you
have to put join in front of gsub, but that might become a performance
nightmare for larger files as you will be calling gsub on *huge*
strings.

HTH
Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

2 Answers

Ian Whitlock

6/30/2007 1:46:00 PM

0

Robert Dober wrote:

> def remove_nl_and_kw file_name, key_word="ruby"
> File.readlines(file_name).
> map{|line| line.chomp}.
> gsub(key_word, "_" << key_word << "_").
> join
>
> end
>
> If you had the newlines removed because keywords might spwan lines you
> have to put join in front of gsub, but that might become a performance
> nightmare for larger files as you will be calling gsub on *huge*
> strings.

Robert,

Nice tight illustrative code, but it needs a newbie correction. If join
is to be at the end, then you need

def remove_nl_and_kw( file_name, key_word="ruby")
File.readlines(file_name).
map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
join
end

Now I have another newbie question. When is the file closed?
end of readlines?
end of statement?
end of function call?
end of program?

Thanks for patience.
Ian

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

Robert Dober

6/30/2007 2:37:00 PM

0

On 6/30/07, Ian Whitlock <iw1junk@comcast.net> wrote:
> Robert Dober wrote:
>
> > def remove_nl_and_kw file_name, key_word="ruby"
> > File.readlines(file_name).
> > map{|line| line.chomp}.
> > gsub(key_word, "_" << key_word << "_").
> > join
> >
> > end
> >
> > If you had the newlines removed because keywords might spwan lines you
> > have to put join in front of gsub, but that might become a performance
> > nightmare for larger files as you will be calling gsub on *huge*
> > strings.
>
> Robert,
>
> Nice tight illustrative code, but it needs a newbie correction. If join
> is to be at the end, then you need
Well spotted, thx for correcting it :), will teach me to copy lines around.
>
> def remove_nl_and_kw( file_name, key_word="ruby")
> File.readlines(file_name).
> map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
that's it very nice
> join
> end
>
> Now I have another newbie question. When is the file closed?
> end of readlines?
that's it, as a matter of fact this is one of my favorite properties
of the class methods
#IO.read, #IO.readlines and #IO.open with a block.
of program?
>
> Thanks for patience.
> Ian
Thank *you*
Robert


--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck