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