CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
To many if's

 



Pro_4
User

Jun 17, 2001, 11:08 AM

Post #1 of 6 (1473 views)
To many if's Can't Post

Hey,

I am working on a little game just to test my skills and all that nice stuff but anyways let me explain what i am trying to do. I am taking whatever people type in the ms dos prompt and comparing it to the possible actions. Say if they typed look house. It would go through the actions and if $command eq "look house" it will print out something like "The house is 3 stories with a white picket fence around it.". It works great but soon i am afraid there will be to many if statements in the subroutine ACTIONS(). Already i have noticed that there is a slowdown in the program with only 20 possible actions. If the game had 1000 actions the program would take all day comparing $command with whatever. Is there anyway to do what i am talking about at faster speeds? I attached just the ACTIONS part of it because it is a forever long script. Also instead of including all the contents of the subroutine ACTIONS should i just read it in from a file that holds all the actions? Anyways thanks a lot for the help.

:)

Yes i am a palm tree. What are you plantist or something!?


randor
User

Jun 17, 2001, 2:14 PM

Post #2 of 6 (1470 views)
Re: To many if's [In reply to] Can't Post

Pro_4,

i have some serious ideas for you to ponder on that, as i too would eventually like to build an idea like that, but i am in a hurry, so when i get back (in a day or two) i will write to you and share them if you care to listen..

I Hope this helps...

perl programmers don't die.. they just start writing a new script.


Mortimer
journeyman

Jun 18, 2001, 3:13 AM

Post #3 of 6 (1462 views)
Re: To many if's [In reply to] Can't Post

I've taken a section of your code, the if statements from...


Code
if ((($command eq 'e') || ($command eq 'east')) && $place eq "bakery") {

...to...


Code
if ((($command eq 'list') || ($command eq 'l')) && $place eq "bakery") {

...to try and show how you can avoid some brainwork, and save billions of dollars' worth of time
in the future.

If you organise your data something like this...


Code
my $command = 's'; 
my $place = 'opening';

my %list = ( 'e' => { 'bakery' => { 'message' => 'You are in the town market. You can travel south and west. To the west is a local bakery.',
'place' => 'town market' },
'forest' => { 'message' => 'You just fell in a giant hole. There is a skeleton laying here.',
'place' => 'pit' } },

's' => { 'opening' => { 'message' => 'You have come upon a large forest. You can go north and east.',
'place' => 'forest' },
'town square' => { 'message' => 'You see a large forest to the south. You can go north and south.',
'place' => 'opening' },
'town market' => { 'message' => 'You are in the Town square. You can travel north and south. There is a shiny sword laying here.',
'place' => 'town square' } },

'n' => { 'opening' => { 'message' => 'You are in the Town square. You can travel north and south. There is a shiny sword laying here.',
'place' => 'town square' } },

'w' => { 'town market' => { 'message' => 'You have entered the bakery. A chubby man sits in front of you. Type list to see what you can buy. The only exit is east.',
'place' => 'bakery' } },

'l' => { 'bakery' => { 'message' => 'Baker: Hello this is what i have to offer. Food | Cost Sweet rolls | 100 Cake | 120 Muffins | 80' } } );

Then, after assigning $command and $place as required, all you need to do in your program is...


Code
print "Message: $list{ $command }{ $place }{'message'} - Place: $list{ $command }{ $place }{'place'}";

You don't have to physically build this hash as above, you can pull all of the keys and values from anywhere. I realize that this may not be the answer to your specific question, but if you can understand hashes and references, you'll never have this sort of problem gain. There's an article by Jasmine here...
http://tlc.perlarchive.com/articles/perl/djm0001.shtml

Hope this helps,
Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




Pro_4
User

Jun 18, 2001, 6:47 AM

Post #4 of 6 (1460 views)
Re: To many if's [In reply to] Can't Post

Hey
Thanks that is a great idea, do you know if there are any speed issues for %hashes? I am assuming it would be much faster than going through every if statement and comparing. Thanks again, let me try this and maybe it will improve the speeds and just save me a lot of typing.

Yes i am a palm tree. What are you plantist or something!?


Mortimer
journeyman

Jun 19, 2001, 1:26 PM

Post #5 of 6 (1440 views)
Re: To many if's [In reply to] Can't Post

In this little example, the hash will usually be slightly faster. If you change the innermost hashes to indexed arrays, you may get a little more speed. The keys are repetetive here so it would probably be more appropriate. You could experiment with the Benchmark module. If you do, I'de be interested to see your results.

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




Pro_4
User

Jun 19, 2001, 5:53 PM

Post #6 of 6 (1434 views)
Re: To many if's [In reply to] Can't Post

I attempted to benchmark them but for some reason i kept getting errors although they work fine when not in the benchmarking and the benchmarking program works fine without them in it. It is probably something stupid i did wrong but i could get it to work..

:)

Yes i am a palm tree. What are you plantist or something!?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives