A little help on finding the closest match

Discussion in 'Ruby' started by trans. (T. Onoma), Oct 22, 2004.

  1. 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.
     
    trans. (T. Onoma), Oct 22, 2004
    #1
    1. Advertising

  2. trans.  (T. Onoma)

    Guest

    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
     
    , Oct 22, 2004
    #2
    1. Advertising

  3. On Thursday 21 October 2004 10:21 pm, 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.
     
    trans. (T. Onoma), Oct 22, 2004
    #3
  4. On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
    | On Thursday 21 October 2004 10:21 pm, 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.
     
    trans. (T. Onoma), Oct 22, 2004
    #4
  5. "trans. (T. Onoma)" <> schrieb im Newsbeitrag
    news:...
    > On Thursday 21 October 2004 10:49 pm, trans. (T. Onoma) wrote:
    > | On Thursday 21 October 2004 10:21 pm, 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
     
    Robert Klemme, Oct 22, 2004
    #5
  6. 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 ;)
     
    trans. (T. Onoma), Oct 22, 2004
    #6
  7. trans.  (T. Onoma)

    MiG Guest

    Is it possible to open file by inode on Linux?

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

    thx,
    Jan Molic
     
    MiG, Oct 22, 2004
    #7
  8. trans.  (T. Onoma)

    Guest

    Re: Is it possible to open file by inode on Linux?

    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
     
    , Oct 22, 2004
    #8
  9. trans.  (T. Onoma)

    MiG Guest

    Re: Is it possible to open file by inode on Linux?

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

    >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, Oct 22, 2004
    #9
  10. Re: Is it possible to open file by inode on Linux?

    "MiG" <> schrieb im Newsbeitrag
    news:...
    >
    > 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 ""
    > <>:
    >
    > >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, Oct 22, 2004
    #10
  11. trans.  (T. Onoma)

    David Ross Guest

    Re: Is it possible to open file by inode on Linux?

    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/
     
    David Ross, Oct 22, 2004
    #11
  12. Re: Is it possible to open file by inode on Linux?

    On Fri, 22 Oct 2004 22:08:38 +0900, MiG <> wrote:
    > 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.
    --
    Hazzle free packages for Ruby?
    RPA is available from http://www.rubyarchive.org/
     
    Eivind Eklund, Oct 22, 2004
    #12
  13. trans.  (T. Onoma)

    hy2 Guest

    Re: Is it possible to open file by inode on Linux?

    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.
     
    hy2, Oct 22, 2004
    #13
  14. trans.  (T. Onoma)

    MiG Guest

    Re: Is it possible to open file by inode on Linux?


    >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
     
    MiG, Oct 22, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=

    Paging data and highlight closest match

    =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=, Apr 21, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    386
    =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?=
    Apr 22, 2006
  2. Mark Harrison

    closest equivalent of fdopen()?

    Mark Harrison, Aug 28, 2006, in forum: C++
    Replies:
    2
    Views:
    316
    red floyd
    Aug 28, 2006
  3. Replies:
    5
    Views:
    338
  4. Edward Jensen
    Replies:
    6
    Views:
    580
    BartC
    Jun 3, 2009
  5. Mahadev Ittina

    Finding the closest value from a matrix

    Mahadev Ittina, Nov 21, 2009, in forum: Ruby
    Replies:
    6
    Views:
    124
    Mahadev Ittina
    Nov 22, 2009
Loading...

Share This Page