Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

switch stattement

by mtrasp (Acolyte)
on Aug 04, 2009 at 02:33 UTC ( [id://785618]=perlquestion: print w/replies, xml ) Need Help??

mtrasp has asked for the wisdom of the Perl Monks concerning the following question:

hi i am trying to change following code into switch statement please help me
if ($operatingsystem eq "170") { $Folder = "winvista"; $Readme = "readme_winvista.txt"; } elsif ($operatingsystem eq "173") { $Folder = "winvista64"; $Readme = "readme_winvista64.txt"; } elsif ($operatingsystem eq "119") { $Folder = "winvxp"; $Readme = "readme_winxp.txt"; } elsif ($operatingsystem eq "210") { $Folder = "winxp64"; $Readme = "readme_winxp64.txt"; } elsif ($operatingsystem eq "541") { $Folder = "windows*7 graphics"; $Readme = "readme_windows *7 graphics .txt"; } elsif ($operatingsystem eq "525") { $Folder = "winvista64"; $Readme = "windows *7 /windows *7/windows *.txt"; }
i have default value $Folder = "win2k_xp"; $Readme = "readme_2k_xp.txt"

Replies are listed 'Best First'.
Re: switch stattement
by ikegami (Patriarch) on Aug 04, 2009 at 04:09 UTC

    Perl doesn't have a switch statement. Well, it does, but it's called given. Since it's only available since 5.10.0, you might have some backwards compatibility issues.

    Ignore suggestions to use the Switch module. It's author recommends against using it since it's a buggy proof-of-concept module. Here at PerlMonks, we've seen a number of questions about problems caused by Switch. They are basiaclly impossible to debug.

    Besides, a lookup table (hash) would be much more appropriate.

Re: switch stattement
by cdarke (Prior) on Aug 04, 2009 at 03:20 UTC
    use warnings; use strict; # Folder Readme my %oses = ("170" => ["winvista", "readme_winvista.txt"], "173" => ["winvista64", "readme_winvista64.txt"], "119" => ["winvxp", "readme_winxp.txt"], "210" => ["winxp64", "readme_winxp64.txt"], "541" => ["windows*7 graphics", "readme_windows *7 graphic +s .txt"], "525" => ["winvista64", "windows *7 /windows *7/wi +ndows *.txt"]); my $os = '210'; my $Folder = "win2k_xp"; my $Readme = "readme_2k_xp.txt"; if (exists $oses{$os}) { $Folder = $oses{$os}[0]; $Readme = $oses{$os}[1] } print "Folder: $Folder, Readme: $Readme\n";

    Update: Added default handling.
      Thank you..it helped me alot
Re: switch stattement
by merlyn (Sage) on Aug 04, 2009 at 03:01 UTC
    That looks like it'd be far better served with a hash, with a key of $operatingsystem.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: switch stattement
by leighsharpe (Monk) on Aug 04, 2009 at 03:21 UTC
    You're going to be told repeatedly that "In perl, there's more than one way to do it.". And, there is more than one way to do what you want.
    Are you using strict and warnings in your example? The first thing that jumps out at me is that you may have a problem with scoping. If you have a
    my $Folder; my $Readme;
    Before your switch statement, the results may be somewhat different to what you are seeing. (by the way, just what is it doing?)

    You may also want to consider using a hash, if you don't have too many options:
    my %folder_options=('170'=>'winvista', '173'=>'winvista64', '119'=>'winvxp', '210'=>'winxp64', '541'=>'windows*7 graphics', '525'=>'winvista64'); my $Folder=$folder_options{$operatingsystem};

    Another option is to use something like this:
    my $Folder; my $Readme; SWITCH: { ($operatingsystem eq '170') and do { $Folder='winvista'; $Readme='readme_winvista.txt'; last SWITCH; } ($operatingsystem eq '173') and do { $Folder='winvista64'; $Readme='readme_winvista64.txt'; last SWITCH; } }
    etc. etc.
    Or, as already mentioned, use Switch.
    use Switch; my $Folder; my $Readme; switch ($operatingsystem) { case '170' { $Folder='winvista'; $Readme='readme_winvista.txt'; } case '173' { $Folder='winvista64'; $Readme='readme_winvista64.txt'; } }
    etc. etc.
    As a final note, if you're using 5.10, there's always the builtin given/when.

      You may also want to consider using a hash, if you don't have too many options:
      I would argue that you may want to consider using a hash, especially if you have many options. The hash can be built up incrementally across many locations, allowing you to create a plugin-style structure, with the handling for each filetype placed in its own module, each of which adds itself to the hash. Even if the hash is defined entirely in one place, it's more compact than other representations.

      Oh, and don't use Switch.

      Or, as already mentioned, use Switch.

      Oh no, don't. Use given, when if you're running perl 5.10, or use some other nethod, but don't use Switch.pm if you can. It's a source filter so it's slow, prone to weird untractable bugs, won't work in "eval", etc.

      Really Thank you very much .your reply helped me alot Thanks again
Re: switch stattement
by Bloodnok (Vicar) on Aug 04, 2009 at 11:58 UTC
    As ikegami rightly says, you should use the Switch module at your peril - all sorts of dangers lurk therein. You _could_ roll your own poor mans' switch:
    SWITCH: { $operatingsystem eq "170" && do { $Folder = "winvista"; $Readme = "readme_winvista.txt"; last SWITCH; }; $operatingsystem eq "173" && do { $Folder = "winvista64"; $Readme = "readme_winvista64.txt"; last SWITCH; }; $operatingsystem eq "119" && do { $Folder = "winvxp"; $Readme = "readme_winxp.txt"; last SWITCH; }; $operatingsystem eq "210" && do { $Folder = "winxp64"; $Readme = "readme_winxp64.txt"; last SWITCH; }; . . . };
    But, as others have pointed out, the case (pun intended:-) for a hash in these circumstances is overwhelming.

    A user level that continues to overstate my experience :-))
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://785618]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-29 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found