how to put unique lines from regexped file

B

beny 18241

Hi all,

I have such a question, when i open a file doing some grep stuff...

---
File.open('aa.xml').each do |line|
dev_name = ARGV[1]
if line =~ /<prf:CcppAccept>/..line =~ /<\/prf:CcppAccept>/
if line =~ /<rdf:li>(image|audio|video).*<\/rdf:li>/
line.gsub('<rdf:li>image/jpeg', "INSERT INTO VMPOLICY_VALUES
VALUES(\'#{dev_name}\',\'jpginpage\',\'true\',\'#{$ver}\')")).gsub(
"<\/rdf:li>", ";").each do |ss|
----

I recive output like

INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)
INSERT INTO TABLE VALUES(1,1,1)

now i wanted to put only that lines which are uniqe...
INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)


how i can do this ??

i thought about...

puts ss.unque

but its dont work


Please Help

Regards
beny18241
 
B

beny 18241

beny said:
Hi all,

I have such a question, when i open a file doing some grep stuff...

---
File.open('aa.xml').each do |line|
dev_name = ARGV[1]
if line =~ /<prf:CcppAccept>/..line =~ /<\/prf:CcppAccept>/
if line =~ /<rdf:li>(image|audio|video).*<\/rdf:li>/
line.gsub('<rdf:li>image/jpeg', "INSERT INTO VMPOLICY_VALUES
VALUES(\'#{dev_name}\',\'jpginpage\',\'true\',\'#{$ver}\')")).gsub(
"<\/rdf:li>", ";").each do |ss|
----

I recive output like

INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)
INSERT INTO TABLE VALUES(1,1,1)

now i wanted to put only that lines which are uniqe...
INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)


how i can do this ??

i thought about...

puts ss.unque

but its dont work


Please Help

Regards
beny18241


ok i figure it out :)

puts ss unless ss == @prev; @prev = ss

cheers
 
R

Robert Klemme

beny said:
Hi all,

I have such a question, when i open a file doing some grep stuff...

---
File.open('aa.xml').each do |line|
dev_name = ARGV[1]
if line =~ /<prf:CcppAccept>/..line =~ /<\/prf:CcppAccept>/
if line =~ /<rdf:li>(image|audio|video).*<\/rdf:li>/
line.gsub('<rdf:li>image/jpeg', "INSERT INTO VMPOLICY_VALUES
VALUES(\'#{dev_name}\',\'jpginpage\',\'true\',\'#{$ver}\')")).gsub(
"<\/rdf:li>", ";").each do |ss|
----

I recive output like

INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)
INSERT INTO TABLE VALUES(1,1,1)

now i wanted to put only that lines which are uniqe...
INSERT INTO TABLE VALUES(1,1,1)
INSERT INTO TABLE VALUES(1,2,2)


how i can do this ??

i thought about...

puts ss.unque

but its dont work


Please Help

Regards
beny18241


ok i figure it out :)

puts ss unless ss == @prev; @prev = ss

That works only if the input is ordered. For unordered input here's an
efficient way:

require 'set'

unique = Set.new

File.foreach('aa.xml') do |line|
x = ... line ...
puts x if unique.add? x
end

Of course you can also use a Hash for this.

unique = {}

File.foreach('aa.xml') do |line|
x = ... line ...

unique.fetch x do
unique[x] = true
puts x
end
end

Btw, since your input appears to be XML it's probably a better idea to
process it with XML tools like REXML and the like.

Kind regards

robert
 
R

Robert Klemme

That's fine for the plodding proles, but we cossacks always shout the
battle-cry: "We don't need no stinkin' loops!"

puts IO.readlines('aa.xml').map{|s| s.sub( ... )}.uniq

Everything must be done in one fell swoop or not at all!

Strong words, but I hope you are aware that this, taken as a general
rule, is bad advice.

Cheers

robert
 
W

W. James

Robert said:
require 'set'

unique = Set.new

File.foreach('aa.xml') do |line|
x = ... line ...
puts x if unique.add? x
end

That's fine for the plodding proles, but we cossacks always shout the
battle-cry: "We don't need no stinkin' loops!"

puts IO.readlines('aa.xml').map{|s| s.sub( ... )}.uniq

Everything must be done in one fell swoop or not at all!

--
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top