Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: using switch

by sicone (Initiate)
on Sep 29, 2005 at 16:02 UTC ( #496155=note: print w/ replies, xml ) Need Help??


in reply to Re: using switch
in thread using switch

i WAS THINKING OF USING IT IN A RPG IM WORKING ON, WELL IT'S A PROJECT I USING TO LEARN PERL. I WAS GOING TO USE THE SWITCH MODULE FOR THINGS LIKE MOVEMENT. ROUGH EXAMPLE CODE NOT TESTED.

USE SWITCH; SUB DIRECTIONS { WHILE { $DIRECTIONS= (<STDIN>); CHOMP $DIRECTIONS $DIRECTIONS =~ TR/A-Z/a-z/; SWITCH { CASE 'NORTH' {PRINT "$HERONAME HAS LEFT NORTH"} CASE 'SOUTH' {PRINT "$HERONAME HAS LEFT SOUTH"} CASE 'EAST' {PRINT "$HERONAME HAS LEFT EAST"} CASE 'WEST' {PRINT "$HERONAME HAS LEFT WEST"} } } }
BASICALY THATS WHAY IM TRYING TO DO...BASICALY


Comment on Re^2: using switch
Download Code
Replies are listed 'Best First'.
Re^3: using switch
by castaway (Parson) on Sep 29, 2005 at 16:11 UTC
    Sounds like you just need to use dispatching. i.e. make a hash which contains the possible commands/directions as keys, and has strings to output as values. (Or probably more useful, has coderefs as values to be run)

    C.

Re^3: using switch
by sgifford (Prior) on Sep 29, 2005 at 16:10 UTC
    Just use several if statements:
    if ($DIRECTIONS eq 'NORTH') { print "$HERONAME HAS LEFT NORTH\n" } elsif ($DIRECTIONS eq 'SOUTH') { print "$HERONAME HAS LEFT SOUTH\n" } elsif ($DIRECTIONS eq 'EAST') { print "$HERONAME HAS LEFT EAST\n" } elsif ($DIRECTIONS eq 'WEST') { print "$HERONAME HAS LEFT WEST\n" }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://496155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2015-08-01 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls