[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

wired result

Cheyne Li

5/8/2008 8:28:00 PM

Hey, guys.

I'm a new ruby learner. I'm so stuck on the regular expression part.
The following code is a fuction that passed in 2 strings: ftarget, which
is the location of a file to read; fkey is the key to match in the file.

The function simply does open the file, read each line and try to find
if there is a match fkey.

I tested the

i=0
line.scan(fkey){|x|i=i+1}
puts i
if i>0
puts "Found match...\n\r"
end



def fcmp(ftarget,fkey)
@argc=ftarget
puts fkey
unless ftest(ftarget)
puts "Invalid File name"
return false
end
File.open(ftarget, 'r') do |f1|
while line = f1.gets
puts line
i=0
line.scan(fkey){|x|i=i+1}
puts i
if i>0
puts "Found match...\n\r"
end
#return false
end
end
end
--
Posted via http://www.ruby-....

4 Answers

Cheyne Li

5/8/2008 8:31:00 PM

0

Sorry, I wasn't finish when I mistakely posted it.

I tested
i=0
line.scan(fkey){|x|i=i+1}
puts i
if i>0
puts "Found match...\n\r"
end

in other file by assgined fkey and line. it worked

but the function itself couldn't find match at all. I have no idea
what's wrong with it. Is there anyone can help me out? Thanks a lot
--
Posted via http://www.ruby-....

Che

5/8/2008 9:09:00 PM

0

On May 9, 4:31 am, Cheyne Li <happy.go.lucky....@gmail.com> wrote:
> Sorry, I wasn't finish when I mistakely posted it.
>
> I tested
>           i=0
>           line.scan(fkey){|x|i=i+1}
>           puts i
>           if i>0
>             puts "Found match...\n\r"
>           end
>
> in other file by assgined fkey and line. it worked
>
> but the function itself couldn't find match at all. I have no idea
> what's wrong with it. Is there anyone can help me out? Thanks a lot
> --
> Posted viahttp://www.ruby-....

This method actually .. works. Check the values of ftarget and fkey
you use, and also check if it returned falsely by ftest.


Michael Linfield

5/8/2008 9:11:00 PM

0

Cheyne Li wrote:
> Sorry, I wasn't finish when I mistakely posted it.
>
> I tested
> i=0
> line.scan(fkey){|x|i=i+1}
> puts i
> if i>0
> puts "Found match...\n\r"
> end
>
> in other file by assgined fkey and line. it worked
>
> but the function itself couldn't find match at all. I have no idea
> what's wrong with it. Is there anyone can help me out? Thanks a lot

If I'm understanding you correctly, you have lets say an array of keys
you want to find in a file.

fileKeys = ["key1","key44","key5"]

lets just say the file contains a bunch of useless jargon and some keys:

testfile.txt #

stuff
more stuff
key5
useless junk
more useless junk
not a key
key44


Ok, now lets read that file and put the keys in an array called results

results = []

IO.foreach("testfile.txt") {|x| results << x.chomp}
=> nil

Now we compare the arrays to find any matches

results&fileKeys
=> ["key5","key44"]

Regards,

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

Cheyne Li

5/8/2008 9:47:00 PM

0

Michael Linfield wrote:
> If I'm understanding you correctly, you have lets say an array of keys
> you want to find in a file.
>
> fileKeys = ["key1","key44","key5"]
>
> lets just say the file contains a bunch of useless jargon and some keys:
>
> testfile.txt #
>
> stuff
> more stuff
> key5
> useless junk
> more useless junk
> not a key
> key44
>
>
> Ok, now lets read that file and put the keys in an array called results
>
> results = []
>
> IO.foreach("testfile.txt") {|x| results << x.chomp}
> => nil
>
> Now we compare the arrays to find any matches
>
> results&fileKeys
> => ["key5","key44"]
>
> Regards,
>
> - Mac


Hi, Mac

My program is not that complex though.
The file has format like this:

C:\Documents and Settings\cheynel\Desktop\p001hello.txt,puts
C:\Documents and Settings\cheynel\Desktop\hello.rb,print
C:\Documents and Settings\cheynel\Desktop\04.rb,Thank
C:\Documents and Settings\cheynel\Desktop\read.rb,print
C:\Documents and Settings\cheynel\Desktop\class.rb,class
C:\Documents and Settings\cheynel\Desktop\reg.rb,cheyne

So, basically, each line has 2 arguments,which is seperated by ","

the first argument is the location of the test fils, and the second one
is the key word to match corresponding the file. such as, "puts" is need
to be found in poo1hello.txt, not need to be found in hello.

Well, the problem is solved, I found the string read in from a file will
automatically attached with something, which I'm quite sure if it is
newline. but use str.chop! will solve it.

Anyway, Thank you for your reply
--
Posted via http://www.ruby-....