[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String Indexing

Ruby Luva

5/15/2009 12:06:00 PM

Hi all,

I have a piece of data like so:

aaaaaaa
bbbbbbb
ccccccc
123
ddddddd
yes
eeeeeee


I need to find the string yes and the string 123, however I first have
to find the string yes. I have this code to do this task:

stack = "aaaaaaa
bbbbbbb
ccccccc
123
ddddddd
yes
eeeeeee"

string = "yes"
stack.each do |c|
c.each {|line|
line.chomp!;
puts line.strip! if line.include? string}
end


How can I then, using 'yes' as my index to find the string '123'?
Basically I need to first find 'yes' and once discovered, search back to
find '123'. I hoep I explained it well enough.

I have thought about using pop, to pop the values off from an array but
I am not sure how to do this.

Could someone please give me a pointer to achieve this?

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

4 Answers

pjb

5/15/2009 12:37:00 PM

0

Ruby Luva <noderat@hotmail.com> writes:

> Hi all,
>
> I have a piece of data like so:
>
> aaaaaaa
> bbbbbbb
> ccccccc
> 123
> ddddddd
> yes
> eeeeeee
>
>
> I need to find the string yes and the string 123, however I first have
> to find the string yes. I have this code to do this task:
>
> stack = "aaaaaaa
> bbbbbbb
> ccccccc
> 123
> ddddddd
> yes
> eeeeeee"
>
> string = "yes"
> stack.each do |c|
> c.each {|line|
> line.chomp!;
> puts line.strip! if line.include? string}
> end
>
>
> How can I then, using 'yes' as my index to find the string '123'?
> Basically I need to first find 'yes' and once discovered, search back to
> find '123'. I hoep I explained it well enough.
>
> I have thought about using pop, to pop the values off from an array but
> I am not sure how to do this.
>
> Could someone please give me a pointer to achieve this?

Just do it.


(stack = "aaaaaaa
bbbbbbb
123 nay!
ccccccc
123 yeah!
ddddddd
yes yes
yes no
eeeeeee")
(string1 = "123")
(string2 = "yes")
(match1 = "")
(match2 = "")
(stack . each { |line|
(line . chomp!)
((match1 = line) if (line . include? string1))
(if (line . include? string2)
(match2 = line)
(break)
end)})
(printf "one=%s\ntwo=%s\n" , match1 , match2)

one=123 yeah!
two=yes yes

--
__Pascal Bourguignon__

Ruby Luva

5/15/2009 1:11:00 PM

0

Pascal J. Bourguignon wrote:
> Ruby Luva <noderat@hotmail.com> writes:

> one=123 yeah!
> two=yes yes


Hi, thanks for the great, simple solution!!


The problem for me is that I do not know what the second value I am
looking for is... :)

I am making an HTTP request to a site and I know one value to look for.
The value I am interested in changes but is always 2 lines above the
string I CAN find.

This code works: #if I want to retrieve 'a'

data = ['a','b','c']
found = data.index('c')
puts data[found-2]

Sadly I need it to work on a HTTP request so I store my data liek this:

got = Array.new
go = net::HTTP.new(a,b)
...
got = go.get('some_path')
...
i = got.index('some known string')
puts got[i-2] #print the secret string 2 lines above




In theory in my mind I can make it work, but in code, I can't/

I get an error:

undefined method `index' for #<Net::HTTPOK 200 OK readbody=true>
(NoMethodError)


Thanks for spending time looking at my post.
--
Posted via http://www.ruby-....

pjb

5/15/2009 1:36:00 PM

0


Ruby Luva <noderat@hotmail.com> writes:

> The problem for me is that I do not know what the second value I am
> looking for is... :)
>
> I am making an HTTP request to a site and I know one value to look for.
> The value I am interested in changes but is always 2 lines above the
> string I CAN find.
>
> This code works: #if I want to retrieve 'a'
>
> data = ['a','b','c']
> found = data.index('c')
> puts data[found-2]

Well if the data you want to extract is two lines above the line you
match, why didn't you say so sooner? You told only us about "123" and
"yes"...

But on the same principle, you may keep a buffer of the last lines
seen to be able to go back easily:


(stack = "aaaaaaa
bbbbbbb
123 nay!
ccccccc
123 yeah!
ddddddd
yes yes
yes no
eeeeeee")
(target = "yes")
(previousLines = (CircularBuffer . new(2)))
(stack . each { |line|
(line . chomp!)
(if (line . include? target)
(break)
end)
(previousLines . enter line)})
(printf "two lines before the target line is '%s'\n" , (previousLines . nth 1))



Here is for example a CircularBuffer that would do:

(class Node
(attr_accessor :data,:previous,:next)
(def initialize(data)
(@data = data)
(@previous = nil)
(@next = nil)
end)
end)

(class CircularBuffer
(def initialize(maxSize)
(@maxSize = maxSize)
(@size = 0)
(@tail = nil)
end)
(def enter(data)
(if (@size == @maxSize)
(@tail . data= data)
(@tail = (@tail . next))
elsif (@size == 0)
(@tail = (Node . new data))
(@tail . previous= @tail)
(@tail . next= @tail)
(@size = 1)
else
(node = (Node . new data))
(node . previous= (@tail . previous))
(node . next= @tail)
(@tail . previous . next = node)
(@tail . previous = node)
(@size = (@size + 1))
end)
end)
(def nth(index)
(index = (index . modulo @maxSize))
(cur = (@tail . previous))
(while (0 < index)
(cur = (cur . previous))
(index = (index - 1))
end)
(cur . data)
end)
end)



> Sadly I need it to work on a HTTP request so I store my data liek this:
>
> got = Array.new
> go = net::HTTP.new(a,b)

However, if your data is HTML or XML, this kind of data is structured,
and newlines are rather insignificant, so it's brittle to count lines
to find data there. You should rather parse the html (there are Ruby
libraries to do that), and find your data "structurally".

--
__Pascal Bourguignon__

Yossef Mendelssohn

5/15/2009 1:46:00 PM

0

On May 15, 8:40=A0am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> =A0 =A0 =A0 =A0 =A0(@tail =3D (Node . new data))

This is one of my favorite lines of Pascal code.

> =A0 =A0 =A0 =A0 =A0(@tail . next=3D =A0 =A0 @tail)

This is also good.


Pascal, why again do you use Ruby if you obviously hate it so much?

--
-yossef