Most Appropriate Data Structure/Method

P

Pat McDonnell

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

it_says_BALLS_on_your forehead

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

Jürgen Exner

Pat said:
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

Jürgen Exner

it_says_BALLS_on_your forehead said:
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
 
I

it_says_BALLS_on_your forehead

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

jkstill

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.

This looks promising:

http://www.perlmonks.org/index.pl?node=429761
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top