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.

thanks in advance
kev

Kevin S. wrote:

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

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…

Li Chen wrote:

Kevin S. wrote:

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

Kevin S. wrote:

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 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

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 :slight_smile:

Li

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

Li Chen wrote:

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

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

On 4-Nov-06, at 10:51 AM, Kevin S. wrote:

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”.split(’ ', 3)
=> [“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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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

On 11/4/06, Kevin S. [email protected] wrote:

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…

IO.foreach(“foo.txt”) do |line|
puts line.split[1]
end

This is assuming your file is called foo.txt

Li Chen wrote:

kev
string

HTH,
Michael G.

----- Original Message -----
From: “Mike S.” [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

bat rat cat mat pat

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.

Kevin S. wrote:

You could use String#split like this:

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

Robin