www.webdeveloper.com
Results 1 to 13 of 13

Thread: [RESOLVED] Perl Switch?

  1. #1
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687

    resolved [RESOLVED] Perl Switch?

    In PHP, you have something like this:

    PHP Code:
    switch ($variable)
    {
    case 
    1:
    //do this;
    break;
    case 
    2:
    //do that;
    break;
    case 
    3:
    //do something else;
    break;

    Is there an equivalent in Perl?

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Code:
    switch ($variable) {
    	case 1 { do this }
    	case 2 { do that }
    	case 3 { do something else }
    	else   { when all else fails }
    }
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    EDIT: Dealt with one error message, still getting a couple others.

    Code:
    #!usr/bin/perl
    # switchexperiment.perl
    
    use warnings;
    use strict;
    
    my $variable=<STDIN>;
    my $word;
    chomp $variable;
    
    switch ($variable){
    	case=1 {
    		$word="One";
    	}
    	case=2 {
    		$word="Two";
    	}
    	case=3 {
    		$word="Three";
    	}
    	else {
    		$word="Zip";
    	}
    }
    
    print $word;
    Quote Originally Posted by Error
    syntax error at C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl line 11, near "){"
    Execution of C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl aborted due to compilation errors.
    Last edited by Mr Initial Man; 10-16-2006 at 01:07 PM.

  4. #4
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Whoops. Somehow a line came out missing when I pasted the code into the text box. It should have been this:
    Code:
    use Switch;
    
    switch ($variable) {
    	case 1 { do this }
    	case 2 { do that }
    	case 3 { do something else }
    	else   { when all else fails }
    }
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  5. #5
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Can't locate Switch.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl line 6.
    BEGIN failed--compilation aborted at C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl line 6.
    So now what do I do?

  6. #6
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    I would actually suggest that you update your version of Perl. The Switch module is one of the core modules that comes prepackaged with Perl, but it is possible that you have an older release of Perl that did not include it.

    If you do not want to install the latest version of Perl, then you will need to install this module. If you installed ActiveState's ActivePerl, then you can use their Perl Package Manager; if you compiled Perl on your machine, then you should use the CPAN module.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  7. #7
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Is Perl 5.6 old, then?

  8. #8
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    The 5.6.x versions began in 2000 and ended in 2003, so yes.

    The newest release at the moment seems to be 5.8.8.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  9. #9
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Where can I get the latest?

  10. #10
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  11. #11
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Here we go! Thanks!

  12. #12
    Join Date
    Feb 2013
    Posts
    1
    F.Y.I. The switch module is now deprecated. There is now the given-when statement which is much more powerful. Or you can simply is the if-else control structure.

  13. #13
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Holy thread necromancy, Batman! 6+ years old and resolved to boot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles