# Wired result

**URL:** <https://www.ruby-forum.com/t/wired-result/137095>\
**Category:** Ruby\
**Created:** [May 8, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/wired-result/137095 "2008-05-08T22:27:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 8, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/wired-result/137095/1 "2008-05-08T22:27:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 8, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/wired-result/137095/2 "2008-05-08T22:31:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 8, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/wired-result/137095/3 "2008-05-08T23:10:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 8, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/wired-result/137095/4 "2008-05-08T23:13:07Z")

</div>

On May 9, 4:31 am, Cheyne Li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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-forum.com/.

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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 8, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/wired-result/137095/5 "2008-05-08T23:46:52Z")

</div>

Michael L. 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
