[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Help processing a file or array

EdUarDo

3/22/2007 12:26:00 PM

Hello, I'm a newbie, and need some help to process a file.
The file has lines with something like this:

rewrewrwer rrrrrrrrrrrrr aa1 rrrrrrrrrr
rewrwerwer rrrrrrrrrrrrr bb1 rrrrrrrrrr
rwerfwdffsd rrrrrrrrrrrrr cc1 rrrrrrrrrr
ewrwerwerwer rrrrrrrrrrrrr dd1 rrrrrrrrrr
trtretertert rrrrrrrrrrrrr ee1 rrrrrrrrrr

and another file with

aa1
cc1

I'd like to create a new file without lines containing aa1 and cc1

Reading the files and get arrays with the content is easy:

lines = File.new("file1").readlines
tags = File.new("file2).readlines

Is there a Ruby way to remove lines from 'lines' variable which contain tags from 'tags' variable?

--
Eduardo Yáñez Parareda
http://legalizate.bl...
4 Answers

Augie De Blieck Jr.

3/22/2007 12:44:00 PM

0

You'd want to use a regular expression, I think. Probably a nested
loop. I could do this in Perl in about two minutes, but I'm still
adjusting my thinking for Ruby.

lines.each do |line|
tags.each do |tag|
final << line if line !~ /#{tag.chop}/
end
end

Then write the "final" array to whatever file you want to.

I threw in the ".chop" there to get rid of the newline character on the tag

Jano Svitok

3/22/2007 1:00:00 PM

0

On 3/22/07, Eduardo Yáñez Parareda <eduardo.yanezNOSPAM@nospamgmail.com> wrote:
> Hello, I'm a newbie, and need some help to process a file.
> The file has lines with something like this:
>
> rewrewrwer rrrrrrrrrrrrr aa1 rrrrrrrrrr
> rewrwerwer rrrrrrrrrrrrr bb1 rrrrrrrrrr
> rwerfwdffsd rrrrrrrrrrrrr cc1 rrrrrrrrrr
> ewrwerwerwer rrrrrrrrrrrrr dd1 rrrrrrrrrr
> trtretertert rrrrrrrrrrrrr ee1 rrrrrrrrrr
>
> and another file with
>
> aa1
> cc1
>
> I'd like to create a new file without lines containing aa1 and cc1
>
> Reading the files and get arrays with the content is easy:
>
> lines = File.new("file1").readlines
> tags = File.new("file2).readlines
>
> Is there a Ruby way to remove lines from 'lines' variable which contain tags from 'tags' variable?
>
> --
> Eduardo Yáñez Parareda
> http://legalizate.bl...
>

tags_re = Regexp.new("\\b(?:#{tags.map {|t|
Regexp.escape(t.chomp)}.join("|")})\\b")
lines.delete_if {|l| l =~ tags_re }

explanation:

tags_re will have form "\b(?:tag1|tag2|tag3|...|tagn)\b"
\b are to match only whole words, not part of the words.
note that if you put in too many tags you may get errors (regexp too
long or something similar)

the last line deletes all lines that match the regexp.

EdUarDo

3/22/2007 2:51:00 PM

0

> tags_re = Regexp.new("\\b(?:#{tags.map {|t|
> Regexp.escape(t.chomp)}.join("|")})\\b")
> lines.delete_if {|l| l =~ tags_re }

One more time I have to praise Ruby...
Thanks Jan.

--
Eduardo Yáñez Parareda
http://legalizate.bl...

Jano Svitok

3/22/2007 4:43:00 PM

0

On 3/22/07, Eduardo Yáñez Parareda <eduardo.yanezNOSPAM@nospamgmail.com> wrote:
> > tags_re = Regexp.new("\\b(?:#{tags.map {|t|
> > Regexp.escape(t.chomp)}.join("|")})\\b")
> > lines.delete_if {|l| l =~ tags_re }
>
> One more time I have to praise Ruby...
> Thanks Jan.

Now that I look at it: this is more like perl than ruby... Ron's
version is probably a bit slower but much more readable...