Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,613 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 952 people online right now. Registration is fast and FREE... Join Now!




Switch statement in Perl

 
Reply to this topicStart new topic

Switch statement in Perl

enigma-paradox
post 23 Jun, 2008 - 12:52 PM
Post #1


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 8

I've been trying to write a script by using a switch statement. I think I've found two different ways to write one, but neither way seems to work. I remember it saying something about a switch module, but I don't know if I have one of those or if I have to download one.
CODE

#!/usr/bin/perl
use strict
use Switch
do{
print"say something to grandma\n";
my $saying=<>;
my $year= rand(20)+1930;

switch ($saying)
   {
     case (lc($saying)) {print"HUH?!, speak up sonny!";}
     case (uc($saying)) {print"No, not since int($year)!";}
   }
} while($saying ne BYE);

The program is supposed to take in a string and say one thing if it is all in lowercase and another thing if it is all in Upper case. I don't want anyone to correct the code, just point me in the right direction. If I do have to download some sort of module could you tell me where?
I also saw that "Perl 6 will provide a built-in switch statement " and that "In Perl 6 switch will be spelled given, and case will be pronounced when". I also tried it with these key words instead of switch and case, but it still didn't work.

This post has been edited by enigma-paradox: 23 Jun, 2008 - 01:03 PM
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 23 Jun, 2008 - 01:53 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


First off, are you using Perl 5 or Perl 6? If you're using 5, attempting to use 6's switch statement won't work.

You might want to look into something called a "dispatch table" - essentially, you use a hash to perform your switch instead of any sort of statement.
User is offlineProfile CardPM

Go to the top of the page

KevinADC
post 23 Jun, 2008 - 02:11 PM
Post #3


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 213



Thanked 3 times

Dream Kudos: 50
My Contributions


You would have to downlaod and install the Switch module. If you install perl 5.10 it supports switch:

http://perldoc.perl.org/feature.html


Switch statements have never been supported by perl before version 5.10, and are not used much by perl coders. The Switch module has some known bugs. Read the Switch modules documentation for details.
User is offlineProfile CardPM

Go to the top of the page

enigma-paradox
post 24 Jun, 2008 - 11:37 AM
Post #4


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 8

Thank you. I know that I ask alot of questions. Thank you all for answering.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 12 Jul, 2008 - 07:04 AM
Post #5


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


The typical idiom in perl goes something like this:

cpp


my $var = "Name";

for($var) {
/^Name$/ && do { .... };
/^Address$/ && do { .... };
}

# Or something like:
for($var) {
do_something() if /^Name$/;
do_something_else() if /^Address$/;
}



This idiom utilizes the default variable $_;

Use the last statement (similar to break in C) to exit the pseudo-switch block.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:48AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month