A little help on finding the closest match

T

trans. (T. Onoma)

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

nobu.nokada

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("|")}/)
 
T

trans. (T. Onoma)

On Thursday 21 October 2004 10:21 pm, (e-mail address 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.
 
T

trans. (T. Onoma)

21 pm, (e-mail address 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.
 
R

Robert Klemme

trans. (T. Onoma) said:
21 pm, (e-mail address 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
 
T

trans. (T. Onoma)

|
| /(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 ;)
 
N

nobu.nokada

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

MiG

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 address 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.
 
R

Robert Klemme

MiG said:
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 address 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.
 
D

David Ross

MiG said:
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-commentary/vfs.html

linux kernel understandings at:
http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html
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
 
E

Eivind Eklund

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

This is not possible on systems that has Unix semantics for file
permissions, because that would violate the security model.
Directories that prevent access to files below them are not supposed
to be possible to circumvent by guessing inode numbers.

The documents David Ross points at are for the kernel layer.

Eivind.
 
H

hy2

yes, you can do something like :

inode = 32
file_list.each{|f| File.open(f){ your_code } if File.stat(f).ino}

if you are working upon a database, as i suppose,
and you whish to use inodes as primary key be aware
that files are on the same file system.


--
here are more things in heaven and earth,
horatio, than are dreamt of in your philosophy.
 
M

MiG


Not due to memory, I'm just interrested in if it is possible..

Hmm. But I must agree it is lowlevel thing, using it I will break
compatibility with windows, moreover inodes can change after umount -
mount.

jan molic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top