Most Appropriate Data Structure/Method
Most Appropriate Data Structure/Method
am 27.08.2007 18:44:26 von 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.
--
Posted via a free Usenet account from http://www.teranews.com
Re: Most Appropriate Data Structure/Method
am 27.08.2007 18:48:31 von it_says_BALLS_on_your forehead
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.
Re: Most Appropriate Data Structure/Method
am 27.08.2007 18:49:24 von jurgenex
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
Re: Most Appropriate Data Structure/Method
am 27.08.2007 19:10:25 von jurgenex
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
Re: Most Appropriate Data Structure/Method
am 27.08.2007 19:15:47 von it_says_BALLS_on_your forehead
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...
Re: Most Appropriate Data Structure/Method
am 28.08.2007 21:13:10 von jkstill
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