[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Kevin Stolt

11/4/2006 3:52:00 PM

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

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

15 Answers

Li Chen

11/4/2006 4:18:00 PM

0

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

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

Mike Stok

11/4/2006 4:25:00 PM

0


On 4-Nov-06, at 10:51 AM, Kevin Stolt 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.

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 Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





Kevin Stolt

11/4/2006 4:29:00 PM

0

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


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

x1

11/4/2006 4:38:00 PM

0

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

On 11/4/06, Kevin Stolt <sba5414@yahoo.com> 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.....
>
>
> Li Chen wrote:
> > Kevin Stolt 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
>
>
> --
> Posted via http://www.ruby-....
>
>

Li Chen

11/4/2006 5:18:00 PM

0

Kevin Stolt 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 :)

Li

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

Li Chen

11/4/2006 5:32:00 PM

0

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

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

Kevin Stolt

11/4/2006 7:19:00 PM

0

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.

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

Robin Stocker

11/5/2006 6:18:00 AM

0

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

You could use String#split like this:

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

Robin

Bernard Kenik

11/6/2006 1:51:00 AM

0


----- Original Message -----
From: "Mike Stok" <mike@stok.ca>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Saturday, November 04, 2006 11:24 AM
Subject: Re: How could I do this search using .scan - I am a newbie


>
> On 4-Nov-06, at 10:51 AM, Kevin Stolt 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.
>
> 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,
>
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.

Hunter

12/16/2011 11:22:00 AM

0

In article <2016441756345681596.493669anim8rfsk-cox.net@news.easynews.com>,
anim8rfsk@cox.net says...
>
> Hunter <buffhunter@my-deja.com> wrote:
> > In article <1496776425345570856.919428anim8rfsk-cox.net@news.easynews.com>,
> > anim8rfsk@cox.net says...
> >>
> >> Pinstripe Sniper <verysorry@nocando.com> wrote:
> >>> "Obveeus" <Obveeus@aol.com> wrote:
> >>>
> >>>>>> ... Me, I'd look into moving that baby Chapa-eye (yeah what ever)
> >>>>>> into the middle of a lake - like what happened to one of the waves
> >>>>>> before it was installed.
> >>>
> >>>> I'm still a bit confused about what the exit hole (Chapa eye?) does anyway.
> >>>
> >>> Chapa eye was my lazy way of spelling what the natives called the Star
> >>> Gate in the Star Gate shows.
> >>>
> >>>> Clearly, people can come through from the future without that thing existing
> >>>> as the first group came through without it. Does the exit hole merely
> >>>> ground the end point, but not the end time for the passthrough? If so, like
> >>>> I said in another post, bury it under 50 feet of rock so the enemy emerges
> >>>> into a wall of stone. Plopping them into a lake doesn't make much sense
> >>>> unless the Terra Nova crowd really really wants to be able to see the whites
> >>>> of their eyes before killing them personally.
> >>>
> >>> Taylor alluded to a fairly high survival rate amoungst the pilgrims
> >>> who splashed down/up in the lake. If they did that again, most people
> >>> float for at least a bit and could be rescued/contained. And unless
> >>> the armor they send through is really cool and can work underwater, it
> >>> would sink and be out of action. Hopefully, magnificient flying
> >>> machines don't come whizzing out of the chappy ieaaaa.
> >>
> >> Nothing about the Chapa Eye makes sense. If you can move it, why the Hell
> >> is it out in the middle of the jungle in the first place?
> > ----
> > Well admittedly we are assuming it can be moved. I can see why they wouldn't
> > want it in Terra Nova. You don't want to have a time portal opening up near
> > where people live.
>
> Why? Put it outside the fence, and point it outward, but put it in plain
> view of the guard towers; Hell, if only to protect it from the Sixers.
-----
Because tears in the space-time continuum opening up next to your house strikes
me as inherently dangerous. Why take the chance? And now there is the threat of
Lucas's backers coming through with troops. Why have it convenient for an enemy
to deposit his forces right in town, or some other disaster?

Just have it a a couple of kilometers outside of town please.
--
----->Hunter

"No man in the wrong can stand up against
a fellow that's in the right and keeps on acomin'."

-----William J. McDonald
Captain, Texas Rangers from 1891 to 1907