[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

remove regex matched line question

Ikuta Lin

3/25/2008 4:47:00 PM

I wrote the code to query a txt file, and tried remove matched line like
as below
but it not work as well, can someone help?
------------------------
code:

src = File.open("./src.txt").readlines
puts src.inspect
src.collect do |e|
if e=~/^(#)/
src.delete(e)
end
end
puts src.inspect
-------------------------
txt

#/etc/passwd
#/etc/shadow
/etc/group

------------------------
problem
["#/etc/passwd \r\n", "#/etc/shadow \r\n", "/etc/group \r\n"]
["#/etc/shadow \r\n", "/etc/group \r\n"]
------------------
still available?
--
Posted via http://www.ruby-....

12 Answers

WujcioL

3/25/2008 4:54:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Try to use String#chop or String#chomp

2008/3/25, Ikuta Lin <ikuta_lin@clousy.net>:
>
> I wrote the code to query a txt file, and tried remove matched line like
> as below
> but it not work as well, can someone help?
> ------------------------
> code:
>
> src = File.open("./src.txt").readlines
> puts src.inspect
> src.collect do |e|
> if e=~/^(#)/
> src.delete(e)
> end
> end
> puts src.inspect
> -------------------------
> txt
>
> #/etc/passwd
> #/etc/shadow
> /etc/group
>
> ------------------------
> problem
> ["#/etc/passwd \r\n", "#/etc/shadow \r\n", "/etc/group \r\n"]
> ["#/etc/shadow \r\n", "/etc/group \r\n"]
> ------------------
> still available?
>
> --
> Posted via http://www.ruby-....
>
>

F. Senault

3/25/2008 4:56:00 PM

0

Le 25 mars à 17:47, Ikuta Lin a écrit :

> I wrote the code to query a txt file, and tried remove matched line like
> as below but it not work as well, can someone help?

> src.collect do |e|
> if e=~/^(#)/
> src.delete(e)
> end
> end

Yep. The collect method returns a copy of your source array, but
doesn't modify it. OTOH, collect! does what you want. That being said,
I'd use reject! myself :

src.reject! { |e| e =~ /^#/ }

HTH !

Fred
--
Every finger in the room is pointing at me I wanna spit in their faces
Then I get afraid of what that could bring I got a bowling ball in my
stomach I got a desert in my mouth Figures that my courage would
choose to sell out now (Tori Amos, Crucify)

Stefano Crocco

3/25/2008 4:56:00 PM

0

On Tuesday 25 March 2008, Ikuta Lin wrote:
> I wrote the code to query a txt file, and tried remove matched line like
> as below
> but it not work as well, can someone help?
> ------------------------
> code:
>
> src = File.open("./src.txt").readlines
> puts src.inspect
> src.collect do |e|
> if e=~/^(#)/
> src.delete(e)
> end
> end
> puts src.inspect
> -------------------------
> txt
>
> #/etc/passwd
> #/etc/shadow
> /etc/group
>
> ------------------------
> problem
> ["#/etc/passwd \r\n", "#/etc/shadow \r\n", "/etc/group \r\n"]
> ["#/etc/shadow \r\n", "/etc/group \r\n"]
> ------------------
> still available?

If I understand you correctly, you want to remove from the array src all the
lines which start with #. In this case, you want reject:

src.delete_if{|l| l.match =~/^#/}

It's usually unwise to remove (and maybe also to add) items to an array while
iterating on it using each or similar methods.

I hope this helps

Stefano


Ikuta Lin

3/25/2008 5:01:00 PM

0

Mateusz Tybura wrote:
> Try to use String#chop or String#chomp
>
> 2008/3/25, Ikuta Lin <ikuta_lin@clousy.net>:

still has problem:(
------------
code

src=src.collect do |e|
e.chop
e.chomp
end
-----------
output

["#/etc/shadow ", "/etc/group "]
~~~~~~~~~~~~~~~~~
--
Posted via http://www.ruby-....

Ikuta Lin

3/25/2008 5:09:00 PM

0

It works !!
Ill remember the usage of iterating.
Thanks for your kindly assistance!

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

WujcioL

3/25/2008 5:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

If you want to show one element in one line try it:

a.each {|s| puts s}

If you want to take # from text use String#delete

2008/3/25, Ikuta Lin <ikuta_lin@clousy.net>:
>
> Mateusz Tybura wrote:
> > Try to use String#chop or String#chomp
> >
> > 2008/3/25, Ikuta Lin <ikuta_lin@clousy.net>:
>
>
> still has problem:(
> ------------
> code
>
> src=src.collect do |e|
> e.chop
> e.chomp
> end
> -----------
> output
>
> ["#/etc/shadow ", "/etc/group "]
> ~~~~~~~~~~~~~~~~~
>
> --
>
> Posted via http://www.ruby-....
>
>

WujcioL

3/25/2008 5:21:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

We are here to serve and protect ;-)

2008/3/25, Ikuta Lin <ikuta_lin@clousy.net>:
>
> It works !!
> Ill remember the usage of iterating.
> Thanks for your kindly assistance!
>
>
> --
> Posted via http://www.ruby-....
>
>

James Gray

3/25/2008 5:33:00 PM

0

On Mar 25, 2008, at 12:12 PM, Mateusz Tybura wrote:

> If you want to show one element in one line try it:
>
> a.each {|s| puts s}

Or the even easier:

puts a

;)

James Edward Gray II

Ryan Davis

3/25/2008 5:50:00 PM

0

You're not rejecting anything complicated, so it is just as easy to
select what you want instead of rejecting what you don't want:

puts File.read("./src.txt").grep(/^[^#]/).join("\n")


Robert Klemme

3/26/2008 12:15:00 PM

0

2008/3/25, Ryan Davis <ryand-ruby@zenspider.com>:
> You're not rejecting anything complicated, so it is just as easy to
> select what you want instead of rejecting what you don't want:
>
> puts File.read("./src.txt").grep(/^[^#]/).join("\n")

You can as well do this which is a tad shorter and probably also more
efficient than reading into a single String:

puts File.readlines("./src.txt").grep(/^[^#]/)

Kind regards

robert

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