Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A little help on finding the closest match

Reply
Thread Tools

A little help on finding the closest match

 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-22-2004
Better way to find the closest match?

# example data
str = "some string abc xyz"
tokens = [ /abc/, /xyz/ ]
i = 0

# must be a better way?
token,match,mindex = tokens.inject([nil,nil,str.length]){ |memo, tkn|
s = str.index( tkn.start_pattern, i )
if s
s < memo[2] ? [tkn, $~, s] : memo
else
memo
end
}

In this case this should return a match to 'abc' (this case has not been
tested, but the core of this example has)

Thanks,
T.


 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      10-22-2004
Hi,

At Fri, 22 Oct 2004 10:38:24 +0900,
trans. (T. Onoma) wrote in [ruby-talk:117331]:
> # example data
> str = "some string abc xyz"
> tokens = [ /abc/, /xyz/ ]

str.index(/#{tokens.join("|")}/)

--
Nobu Nakada


 
Reply With Quote
 
 
 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-22-2004
On Thursday 21 October 2004 10:21 pm, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
| Hi,
|
| At Fri, 22 Oct 2004 10:38:24 +0900,
|
| trans. (T. Onoma) wrote in [ruby-talk:117331]:
| > # example data
| > str = "some string abc xyz"
| > tokens = [ /abc/, /xyz/ ]
|
| str.index(/#{tokens.join("|")}/)

Nice! --I knew I was overlooking something.

Thank You Very Much,
T.


 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-22-2004
On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
| On Thursday 21 October 2004 10:21 pm, (E-Mail Removed) wrote:
| | Hi,
| |
| | At Fri, 22 Oct 2004 10:38:24 +0900,
| |
| | trans. (T. Onoma) wrote in [ruby-talk:117331]:
| | > # example data
| | > str = "some string abc xyz"
| | > tokens = [ /abc/, /xyz/ ]
| |
| | str.index(/#{tokens.join("|")}/)

Actually, I'm still playing with it, but it looks like this won't work b/c I
have subexpressions in my actual tokens. e.g.

[ /()(abc)(\S)/, ... ]

And the match indexes seem to get lost when I join them. Also, I'm not sure
how to tell which token it was that actually matched.

I'll keep at it. Thanks again.
T.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-22-2004

"trans. (T. Onoma)" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
> | On Thursday 21 October 2004 10:21 pm, (E-Mail Removed) wrote:
> | | Hi,
> | |
> | | At Fri, 22 Oct 2004 10:38:24 +0900,
> | |
> | | trans. (T. Onoma) wrote in [ruby-talk:117331]:
> | | > # example data
> | | > str = "some string abc xyz"
> | | > tokens = [ /abc/, /xyz/ ]
> | |
> | | str.index(/#{tokens.join("|")}/)
>
> Actually, I'm still playing with it, but it looks like this won't work

b/c I
> have subexpressions in my actual tokens. e.g.
>
> [ /()(abc)(\S)/, ... ]


If you're just interested in the tokens you probably want this:

/(token1)|(token2(?:sub2))/

i.e. use capturing groups for tokens and non capturing groups for all sub
groups.

> And the match indexes seem to get lost when I join them. Also, I'm not

sure
> how to tell which token it was that actually matched.


You can use Regexp#match. Or use scan in a method with return in the
block.

robert

 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-22-2004
On Friday 22 October 2004 04:19 am, Robert Klemme wrote:
| If you're just interested in the tokens you probably want this:
|
| /(token1)|(token2(?:sub2))/
|
| i.e. use capturing groups for tokens and non capturing groups for all sub
| groups.

Indeed! Thank You. I was doing this:

/(sub)(token)(?=sub)/

But looking over my code I think your expression may work better. I will try
and see.

| > And the match indexes seem to get lost when I join them. Also, I'm not
| > sure how to tell which token it was that actually matched.
|
| You can use Regexp#match. Or use scan in a method with return in the
| block.

Okay, I'll look into this too.

Thanks again,
T.


(God willing, I may actually finish this program in my lifetime


 
Reply With Quote
 
MiG
Guest
Posts: n/a
 
      10-22-2004

Is it possible to open file if I know it's inode (on Linux)?

thx,
Jan Molic



 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      10-22-2004
Hi,

At Fri, 22 Oct 2004 22:08:38 +0900,
MiG wrote in [ruby-talk:117351]:
> Is it possible to open file if I know it's inode (on Linux)?


Although here is not ML for Linux, you could do it by searching
the corresponding file name from the root.

--
Nobu Nakada


 
Reply With Quote
 
MiG
Guest
Posts: n/a
 
      10-22-2004

Of course, but it's not what i mean.
My program firstly scan the tree, but I don't want to cache names, just
inodes..
After the scan need to open some files.

jan molic

Dne 22/10/2004, napsal "(E-Mail Removed)"
<(E-Mail Removed)>:

>Hi,
>
>At Fri, 22 Oct 2004 22:08:38 +0900,
>MiG wrote in [ruby-talk:117351]:
>> Is it possible to open file if I know it's inode (on Linux)?

>
>Although here is not ML for Linux, you could do it by searching
>the corresponding file name from the root.
>
>--
>Nobu Nakada
>




 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-22-2004

"MiG" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>
> Of course, but it's not what i mean.
> My program firstly scan the tree, but I don't want to cache names, just
> inodes..


Why is that? Memory?

> After the scan need to open some files.


If you give a bit more information we might be able to come up with a
solution.

Kind regards

robert

>
> jan molic
>
> Dne 22/10/2004, napsal "(E-Mail Removed)"
> <(E-Mail Removed)>:
>
> >Hi,
> >
> >At Fri, 22 Oct 2004 22:08:38 +0900,
> >MiG wrote in [ruby-talk:117351]:
> >> Is it possible to open file if I know it's inode (on Linux)?

> >
> >Although here is not ML for Linux, you could do it by searching
> >the corresponding file name from the root.
> >
> >--
> >Nobu Nakada
> >

>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the closest value from a matrix Mahadev Ittina Ruby 6 11-22-2009 08:44 PM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
Paging data and highlight closest match =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= ASP .Net 2 04-22-2006 12:41 AM
closest macro-focusing ability oo OLD or new digital cameras? (forpostage stamps) dave Digital Photography 14 11-13-2005 01:20 AM
The 'rewards' of being their 'closest allies'... gb Computer Support 13 01-11-2004 12:25 AM



Advertisments