Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Most Appropriate Data Structure/Method

Reply
Thread Tools

Most Appropriate Data Structure/Method

 
 
Pat McDonnell
Guest
Posts: n/a
 
      08-27-2007
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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 12:44 pm, Pat McDonnell <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-27-2007
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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-27-2007
it_says_BALLS_on_your forehead wrote:
> On Aug 27, 12:44 pm, Pat McDonnell <(E-Mail Removed)> 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


 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 1:10 pm, "Jürgen Exner" <(E-Mail Removed)> wrote:
> it_says_BALLS_on_your forehead wrote:
> > On Aug 27, 12:44 pm, Pat McDonnell <(E-Mail Removed)> 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...

 
Reply With Quote
 
jkstill
Guest
Posts: n/a
 
      08-28-2007
On Aug 27, 9:44 am, Pat McDonnell <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me choose appropriate data structure bigbang C Programming 24 06-04-2010 04:18 PM
Chris Uppal gives the most appropriate replies hthukral.mickey@gmail.com Java 2 02-22-2007 08:54 PM
selecting and working with appropriate data type lane straatman C Programming 13 01-09-2007 06:55 PM
[OT] Which technology is most appropriate for this web application? annunaki Java 0 05-24-2005 11:26 PM
Invoking the Constructor of the Top Most Class in the Hierarchy from the Bottom most class H.MuthuKumaraRajan Java 3 02-04-2004 01:33 PM



Advertisments