How could I do this search using .scan - I am a newbie

K

Kevin Stolt

Hi,

I am new to ruby and I have been search and pulling my hair to do this
using .scan?

I have file which contains.....(this is an example)

bat rat cat mat pat
mat sat put tomorrow today


So how could I search so I could get the values in the second column?

I want to find "rat" and "sat" for example. I searched many time and
read many things but I can't figure out. I hope someone would be kind
enough to help me or show me the correct direction.

any help is appreciated.

thanks in advance
kev
 
L

Li Chen

Kevin said:
Hi,

I am new to ruby and I have been search and pulling my hair to do this
using .scan?

I have file which contains.....(this is an example)

bat rat cat mat pat
mat sat put tomorrow today


So how could I search so I could get the values in the second column?

I want to find "rat" and "sat" for example. I searched many time and
read many things but I can't figure out. I hope someone would be kind
enough to help me or show me the correct direction.

any help is appreciated.

thanks in advance
kev

I don't know what you want but the following might show you some clues:

C:\>irb
irb(main):001:0> line='bat rat cat mat pat'
=> "bat rat cat mat pat"
irb(main):002:0> re=Regexp.new('rat') #create a regular expression
abject
=> /rat/
irb(main):004:0> puts re.match(line) # match the regexp against the
string
rat
=> nil


Li
 
M

Mike Stok

Hi,

I am new to ruby and I have been search and pulling my hair to do this
using .scan?

I have file which contains.....(this is an example)

bat rat cat mat pat
mat sat put tomorrow today


So how could I search so I could get the values in the second column?

I want to find "rat" and "sat" for example. I searched many time and
read many things but I can't figure out. I hope someone would be kind
enough to help me or show me the correct direction.

any help is appreciated.

You might want to look at String#split, e.g.
=> ["bat", "rat", "cat mat pat"]

and pull out the second element, or you could use a regular expression
"bat rat cat mat pat".match(/\s(\S+)\s/)[1]
=> "rat"

These are just suggestions, and if you care about efficiency and
robustness then you should understand them and see if they offer any
good starting points.

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
K

Kevin Stolt

Thx for ur help....but this is just searching for 'rat' isn't it?
thats not going to work>

what I am looking is to find anything in the second column. I am not
sure how to explain otherwise.....
 
X

x1

puts %w(bat rat cat mat pat)[1]

Thx for ur help....but this is just searching for 'rat' isn't it?
thats not going to work>

what I am looking is to find anything in the second column. I am not
sure how to explain otherwise.....
 
L

Li Chen

Kevin said:
Thx for ur help....but this is just searching for 'rat' isn't it?
thats not going to work>

what I am looking is to find anything in the second column. I am not
sure how to explain otherwise.....

I assume you read a big file called test.txt with many lines in this
format
...
A B C D E F...
1 2 3 4 5 6 ...
11 22 33 44 55 66 ...
...

Each line is separated with \t (or whatever)

Then you want to get everything from the second cloumn such as
B 2 22,.. and put them to screen

Here we go:

###file_read1.rb


require 'enumerator'

file_name='test.txt'
result=Array.new

##append second column into an array
File.open(file_name).each do |line|
result <<line.chomp.split(/\t/)[1]
end

#print the result in 6 columns/row separated by \t using nested
iterators
result.each_slice(6) do|slice|
slice.each{|element|print "#{element}\t"}
puts
end


###output
ruby read_file1.rb 1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19
Exit code: 0



########test.txt#######
A 1 C D E F
1 2 3 4 5 6
a 3 c d e f
A 4 C D E F
1 5 3 4 5 6
a 6 c d e f
A 7 C D E F
1 8 3 4 5 6
a 9 c d e f
A 10 C D E F
1 11 3 4 5 6
a 12 c d e f
A 13 C D E F
1 14 3 4 5 6
a 15 c d e f
A 16 C D E F
1 17 3 4 5 6
a 18 c d e f
A 19 C D E F



#######

What is the differnce between Perl and Ruby?
In Perl you need to do many things by yourself but in Ruby you just tell
it what you want and Ruby does it for you :)

Li
 
L

Li Chen

Li Chen wrote:> ...
A B C D E F...
1 2 3 4 5 6 ...
11 22 33 44 55 66 ...
...

Each line is separated with \t (or whatever)

Then you want to get everything from the second cloumn such as
B 2 22,.. and put them to screen

Typo,should be written as "columns on each line are separated with \t
(or whatever)"

Li
 
K

Kevin Stolt

Li said:
Li Chen wrote:> ...

Typo,should be written as "columns on each line are separated with \t
(or whatever)"

Li


dude thanks so...much...I was trying so hard....trying to do this....ur
my hero.
 
R

Robin Stocker

Kevin said:
Hi,

I am new to ruby and I have been search and pulling my hair to do this
using .scan?

I have file which contains.....(this is an example)

bat rat cat mat pat
mat sat put tomorrow today


So how could I search so I could get the values in the second column?

I want to find "rat" and "sat" for example. I searched many time and
read many things but I can't figure out. I hope someone would be kind
enough to help me or show me the correct direction.

any help is appreciated.

thanks in advance
kev

You could use String#split like this:

ARGF.each_line do |line|
puts line.split[1]
end

Robin
 
B

Bernard Kenik

----- Original Message -----
From: "Mike Stok" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Saturday, November 04, 2006 11:24 AM
Subject: Re: How could I do this search using .scan - I am a newbie

Hi,

I am new to ruby and I have been search and pulling my hair to do this
using .scan?

I have file which contains.....(this is an example)

bat rat cat mat pat
mat sat put tomorrow today


So how could I search so I could get the values in the second column?

I want to find "rat" and "sat" for example. I searched many time and
read many things but I can't figure out. I hope someone would be kind
enough to help me or show me the correct direction.

any help is appreciated.

You might want to look at String#split, e.g.
=> ["bat", "rat", "cat mat pat"]

and pull out the second element, or you could use a regular expression
"bat rat cat mat pat".match(/\s(\S+)\s/)[1]
=> "rat"

These are just suggestions, and if you care about efficiency and
robustness then you should understand them and see if they offer any
good starting points.

Hope this helps,
It appears that the OP wants the 2nd word in a string
for his examples, you can accomplish it with 'scan' as follows
'bat rat cat mat pat'.scan(/\w+\)[1]
=> "rat"
alternately you can use '.split' in a similar manner
'bat rat cat mat pat'.split(/\s/)[1] or 'bat rat cat mat'.split(' ')[1]
both yield => "rat"

I would suggest that the OP executes ri String#scan and String#split.
The OP would like learn more this way.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top