[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

A little help on finding the closest match

T. Onoma

10/22/2004 1:38:00 AM

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.


13 Answers

nobu.nokada

10/22/2004 2:22:00 AM

0

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


T. Onoma

10/22/2004 2:50:00 AM

0

On Thursday 21 October 2004 10:21 pm, nobu.nokada@softhome.net 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.


T. Onoma

10/22/2004 3:03:00 AM

0

On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
| On Thursday 21 October 2004 10:21 pm, nobu.nokada@softhome.net 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.


Robert Klemme

10/22/2004 8:17:00 AM

0


"trans. (T. Onoma)" <transami@runbox.com> schrieb im Newsbeitrag
news:200410212302.34298.transami@runbox.com...
> On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
> | On Thursday 21 October 2004 10:21 pm, nobu.nokada@softhome.net 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

T. Onoma

10/22/2004 1:04:00 PM

0

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


MiG

10/22/2004 1:09:00 PM

0


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

thx,
Jan Molic



nobu.nokada

10/22/2004 1:16:00 PM

0

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


MiG

10/22/2004 1:19:00 PM

0


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 "nobu.nokada@softhome.net"
<nobu.nokada@softhome.net>:

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



Robert Klemme

10/22/2004 1:39:00 PM

0


"MiG" <mig@1984.cz> schrieb im Newsbeitrag
news:XqdKn7tA.1098451120.1430970.mig@1984.cz...
>
> 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 "nobu.nokada@softhome.net"
> <nobu.nokada@softhome.net>:
>
> >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
> >
>
>
>

David Ross

10/22/2004 1:57:00 PM

0

MiG wrote:

>Is it possible to open file if I know it's inode (on Linux)?
>
>thx,
>Jan Molic
>
>
>
>
Quite certainly you can access files via inode.

http://www.cse.unsw.edu.au/~neilb/oss/linux-commentar...

linux kernel understandings at:
http://www.tldp.org/LDP/khg/HyperNews/get/fs/vf...
The document says you can understand the filesystem by also reading the
sources in the fs/ folder in your kernel sources. open.c
You need to be able to write good C code and understand much.

David Ross
--
Hazzle free packages for Ruby?
RPA is available from http://www.rubyar...