A little help on finding the closest match

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.

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

Nice! --I knew I was overlooking something.

Thank You Very Much,
T.

···

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.

···

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

"trans. (T. Onoma)" <transami@runbox.com> schrieb im Newsbeitrag
news:200410212302.34298.transami@runbox.com...

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

···

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:

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

···

On Friday 22 October 2004 04:19 am, Robert Klemme wrote:

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

thx,
Jan Molic

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

···

--
Hazzle free packages for Ruby?
RPA is available from http://www.rubyarchive.org/

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.

···

On Fri, 22 Oct 2004 22:08:38 +0900, MiG <mig@1984.cz> wrote:

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

--
Hazzle free packages for Ruby?
RPA is available from http://www.rubyarchive.org/

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.

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

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

Why?

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