Most Appropriate Data Structure/Method

Discussion in 'Perl Misc' started by Pat McDonnell, Aug 27, 2007.

  1. Hello -

    I'm working on a script in which the user provides a path, and the script
    does something based on that path. I want to develop some type of
    association between paths, and actions to perform. For instance, take the
    following:

    Path Action
    ----------------------
    /home &home()
    /home/luser &luser()
    /home/luser/abc &luserabc();

    So, if the user passes /home/luser, &luser would be executed. If the user
    passes /home/luser/xyz, &luser would still be executed, as it is the most
    specific match for that path. If /home/anotheruser is passed, &home would
    be executed, as that is the most specific match. Does this make sense?

    So, my question is, is there any type of data structure/module that is best
    for this? I could hack something together with regex's and loops easily
    enough, but I was hoping there would be something "cleaner." I've looked
    at a trie, but that seems to be the opposite of what I want. Thanks.

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Pat McDonnell, Aug 27, 2007
    #1
    1. Advertising

  2. On Aug 27, 12:44 pm, Pat McDonnell <> wrote:
    > Hello -
    >
    > I'm working on a script in which the user provides a path, and the script
    > does something based on that path. I want to develop some type of
    > association between paths, and actions to perform. For instance, take the
    > following:
    >
    > Path Action
    > ----------------------
    > /home &home()
    > /home/luser &luser()
    > /home/luser/abc &luserabc();
    >
    > So, if the user passes /home/luser, &luser would be executed. If the user
    > passes /home/luser/xyz, &luser would still be executed, as it is the most
    > specific match for that path. If /home/anotheruser is passed, &home would
    > be executed, as that is the most specific match. Does this make sense?
    >
    > So, my question is, is there any type of data structure/module that is best
    > for this? I could hack something together with regex's and loops easily
    > enough, but I was hoping there would be something "cleaner." I've looked
    > at a trie, but that seems to be the opposite of what I want. Thanks.
    >


    you are looking for a dispatch table. essentially, you can map a key
    (in this case, a string describing a path) to a value (a subroutine
    reference) via a hash.
     
    it_says_BALLS_on_your forehead, Aug 27, 2007
    #2
    1. Advertising

  3. Pat McDonnell wrote:
    > Hello -
    >
    > I'm working on a script in which the user provides a path, and the
    > script does something based on that path. I want to develop some
    > type of association between paths, and actions to perform. For
    > instance, take the following:
    >
    > Path Action
    > ----------------------
    > /home &home()
    > /home/luser &luser()
    > /home/luser/abc &luserabc();
    >
    > So, if the user passes /home/luser, &luser would be executed. If the
    > user passes /home/luser/xyz, &luser would still be executed, as it is
    > the most specific match for that path. If /home/anotheruser is
    > passed, &home would be executed, as that is the most specific match.
    > Does this make sense?
    >
    > So, my question is, is there any type of data structure/module that
    > is best for this? I could hack something together with regex's and
    > loops easily enough, but I was hoping there would be something
    > "cleaner." I've looked at a trie, but that seems to be the opposite
    > of what I want.


    I would use a hash with a classic dispatch table.
    And then iterate over the path, in each iteration chopping of the trailing
    end of the path, until I find a matching key in the table.

    jue
     
    Jürgen Exner, Aug 27, 2007
    #3
  4. it_says_BALLS_on_your forehead wrote:
    > On Aug 27, 12:44 pm, Pat McDonnell <> wrote:
    >> Path Action
    >> ----------------------
    >> /home &home()
    >> /home/luser &luser()
    >> /home/luser/abc &luserabc();
    >>
    >> So, if the user passes /home/luser, &luser would be executed. If
    >> the user passes /home/luser/xyz, &luser would still be executed, as
    >> it is the most specific match for that path. If /home/anotheruser
    >> is passed, &home would be executed, as that is the most specific
    >> match.

    >
    > you are looking for a dispatch table. essentially, you can map a key
    > (in this case, a string describing a path) to a value (a subroutine
    > reference) via a hash.


    Yes, but with a twist: a normal hash doesn't provide the "most specific
    match" functionality

    jue
     
    Jürgen Exner, Aug 27, 2007
    #4
  5. On Aug 27, 1:10 pm, "Jürgen Exner" <> wrote:
    > it_says_BALLS_on_your forehead wrote:
    > > On Aug 27, 12:44 pm, Pat McDonnell <> wrote:
    > >> Path Action
    > >> ----------------------
    > >> /home &home()
    > >> /home/luser &luser()
    > >> /home/luser/abc &luserabc();

    >
    > >> So, if the user passes /home/luser, &luser would be executed. If
    > >> the user passes /home/luser/xyz, &luser would still be executed, as
    > >> it is the most specific match for that path. If /home/anotheruser
    > >> is passed, &home would be executed, as that is the most specific
    > >> match.

    >
    > > you are looking for a dispatch table. essentially, you can map a key
    > > (in this case, a string describing a path) to a value (a subroutine
    > > reference) via a hash.

    >
    > Yes, but with a twist: a normal hash doesn't provide the "most specific
    > match" functionality


    ahh, i hadn't read the OP's post thoroughly enough. man, i'm just off
    today...
     
    it_says_BALLS_on_your forehead, Aug 27, 2007
    #5
  6. Pat McDonnell

    jkstill Guest

    On Aug 27, 9:44 am, Pat McDonnell <> wrote:
    > Hello -
    >
    > I'm working on a script in which the user provides a path, and the script
    > does something based on that path. I want to develop some type of
    > association between paths, and actions to perform. For instance, take the
    > following:
    >
    > Path Action
    > ----------------------
    > /home &home()
    > /home/luser &luser()
    > /home/luser/abc &luserabc();
    >
    > So, if the user passes /home/luser, &luser would be executed. If the user
    > passes /home/luser/xyz, &luser would still be executed, as it is the most
    > specific match for that path. If /home/anotheruser is passed, &home would
    > be executed, as that is the most specific match. Does this make sense?
    >
    > So, my question is, is there any type of data structure/module that is best
    > for this? I could hack something together with regex's and loops easily
    > enough, but I was hoping there would be something "cleaner." I've looked
    > at a trie, but that seems to be the opposite of what I want. Thanks.
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    This looks promising:

    http://www.perlmonks.org/index.pl?node=429761
     
    jkstill, Aug 28, 2007
    #6
    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. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    476
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. annunaki
    Replies:
    0
    Views:
    346
    annunaki
    May 25, 2005
  3. Replies:
    2
    Views:
    341
    RedGrittyBrick
    Feb 22, 2007
  4. xkenneth
    Replies:
    8
    Views:
    362
    Bruno Desthuilliers
    Feb 6, 2008
  5. bigbang

    Help me choose appropriate data structure

    bigbang, Jun 1, 2010, in forum: C Programming
    Replies:
    24
    Views:
    857
    Fire Crow
    Jun 4, 2010
Loading...

Share This Page