remove regex matched line question

I

Ikuta Lin

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
 
M

Mateusz Tybura

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

Try to use String#chop or String#chomp
 
F

F. Senault

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
 
S

Stefano Crocco

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

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
 
I

Ikuta Lin

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

Mateusz Tybura

[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 said:
Mateusz said:
Try to use String#chop or String#chomp

2008/3/25, Ikuta Lin <[email protected]>:


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

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

["#/etc/shadow ", "/etc/group "]
~~~~~~~~~~~~~~~~~
 
M

Mateusz Tybura

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

We are here to serve and protect ;-)
 
R

Ryan Davis

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")
 
R

Robert Klemme

2008/3/25 said:
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
 
R

Ryan Davis

2008/3/25 said:
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(/^[^#]/)

Only shorter because of the lack of join... which I do mostly out of
habit but also because puts is subject to the value of $, . Granted,
it doesn't get modified much, but I prefer to know what is going to be
printed. Muscle memory. *shrug*

I doubt the readlines is more efficient as far as memory goes. 1
string + 1 array + N strings vs 1 array + M strings + 1 array + N
strings. As far as time? I dunno. I could measure but I doubt it makes
much difference in the grand scheme of things.
 
R

Robert Klemme

2008/3/26 said:
2008/3/25 said:
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(/^[^#]/)

Only shorter because of the lack of join... which I do mostly out of
habit but also because puts is subject to the value of $, . Granted,
it doesn't get modified much, but I prefer to know what is going to be
printed. Muscle memory. *shrug*

OTOH, if you globally set $, you intentionally do so and want all
output to use it.
I doubt the readlines is more efficient as far as memory goes. 1
string + 1 array + N strings vs 1 array + M strings + 1 array + N
strings. As far as time? I dunno. I could measure but I doubt it makes
much difference in the grand scheme of things.

I recall one occasion (I believe in one of Dave Thomas's books) where
they could remedy a performance issue by going from String#<< to
Array#<<. I'd say the gain comes from the way mem is reallocated: in
the Array only Array memory is reallocated which is far less than
String mem when you have all in one String.

Of course, memory wise every solution that does not require the whole
file in mem at some point in time is more efficient and more robust
for large files.

Kind regards

robert
 

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

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,120
Latest member
Natbelix

Latest Threads

Top