Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i change into switch statement 3

Status
Not open for further replies.

mtrasp

Programmer
Jun 15, 2009
35
0
0
US
HI please help me how can i change following code into switch statement

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";
}
 
These situations often beg for a lookup table so that we can both keep the code very short and simple, plus the lookup table becomes very simple to maintain.

For example:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$os_lookup[/blue] = [red]{[/red]
        [fuchsia]170[/fuchsia] => [red][[/red] [red]"[/red][purple]winvista[/purple][red]"[/red],     [red]"[/red][purple]readme_winvista.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]173[/fuchsia] => [red][[/red] [red]"[/red][purple]winvista64[/purple][red]"[/red],   [red]"[/red][purple]readme_winvista64.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]119[/fuchsia] => [red][[/red] [red]"[/red][purple]winxp[/purple][red]"[/red],        [red]"[/red][purple]readme_winxp.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]210[/fuchsia] => [red][[/red] [red]"[/red][purple]winxp64[/purple][red]"[/red],      [red]"[/red][purple]readme_winxp64.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]541[/fuchsia] => [red][[/red] [red]"[/red][purple]windows*7[/purple][red]"[/red],    [red]"[/red][purple]readme_windows *7 graphics .txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]525[/fuchsia] => [red][[/red] [red]"[/red][purple]windows*7-64[/purple][red]"[/red], [red]"[/red][purple]windows *7 /windows *7/windows *.txt[/purple][red]"[/red] [red]][/red],
[red]}[/red][red];[/red]

[olive][b]if[/b][/olive][red]([/red][url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$os_lookup[/blue]->[red]{[/red][blue]$operatingsystem[/blue][red]}[/red][red])[/red] [red]{[/red]
    [red]([/red][blue]$Folder[/blue], [blue]$Readme[/blue][red])[/red] = [blue]@[/blue][red]{[/red][blue]$os_lookup[/blue]->[red]{[/red][blue]$operatingsystem[/blue][red]}[/red][red]}[/red][red];[/red]
[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
    [gray][i]# Here you should handle failed match in some way[/i][/gray]
[red]}[/red]


Trojan.
 
Two more things:
Firstly, I didn't copy your data perfectly there, I kinda made some assumptions and it doesn't match your original so watch out for that.
Secondly, if there is any risk that you might like to add more data to the structure at a later date then I would be tempted to swap the array refs for hash refs and use keys like "folder" and "readme" to make the data items more obvious.

HTH.


Trojan.
 
Just for interest, there *is* a Switch statement available in perl. I've used it on a number of occasions and although I love the concept and the syntax, I've had it cause some very weird bugs that were practically untraceable.

As a consequence I've had to replace it with hash lookups or similar in virtually all cases and therefore I could not recommend that anyone use it in production code.



Trojan.
 
I still stand by my suggestion of a lookup hash in this case though! ;-)


Trojan.
 
I've always just abused a for loop to handle switch statements:

Code:
for ($operatingsystem) {
   /^170$/ && do {
      $Folder = "winvista";
      $Readme = "readme_winvista.txt";
      last;
   };

   /^173$/ && do {
      $Folder = "winvista64";
      $Readme = "readme_winvista64.txt";
      last;
   };

   # and so-on

   # any code outside of that format can be considered
   # the "default" case for a switch statement
}

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Hey Kirsle,

That's a nice trick if you need to do different types of test and/or regex tests but it's a little cumbersome for a simple literal lookup like this.

Nice idea though.



Trojan.
 
Kirl...... I got dizzy looking at your signature. My vision is still fuzzy......... hehe


[root@netwatch ~]# yum remove windows
Loaded plugins: fastestmirror
Setting up Remove Process
No Match for argument: windows
No Packages marked for removal

OH YEAH!
 
HI Trojan,
I am new to perl scripting can you please tell me more about look up table . i have default value $Folder = "win2k_xp";
$Readme = "readme_2k_xp.txt"; where can i put these two default values .in look up table or in else statement? my boss is asking me to write in switch statement only please help me
 
Bosses eh? ;-)

The default should be in the "else" clause.
I've reworked the code to match your original values but I've added a couple of my concerns in the comments:

Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$os_lookup[/blue] = [red]{[/red]
        [fuchsia]170[/fuchsia] => [red][[/red] [red]"[/red][purple]winvista[/purple][red]"[/red],           [red]"[/red][purple]readme_winvista.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]173[/fuchsia] => [red][[/red] [red]"[/red][purple]winvista64[/purple][red]"[/red],         [red]"[/red][purple]readme_winvista64.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]119[/fuchsia] => [red][[/red] [red]"[/red][purple]winvxp[/purple][red]"[/red],             [red]"[/red][purple]readme_winxp.txt[/purple][red]"[/red] [red]][/red],                      [gray][i]# Are we sure this should be winvxp and not winxp ?[/i][/gray]
        [fuchsia]210[/fuchsia] => [red][[/red] [red]"[/red][purple]winxp64[/purple][red]"[/red],            [red]"[/red][purple]readme_winxp64.txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]541[/fuchsia] => [red][[/red] [red]"[/red][purple]windows*7 graphics[/purple][red]"[/red], [red]"[/red][purple]readme_windows *7 graphics .txt[/purple][red]"[/red] [red]][/red],
        [fuchsia]525[/fuchsia] => [red][[/red] [red]"[/red][purple]winvista64[/purple][red]"[/red],         [red]"[/red][purple]windows *7 /windows *7/windows *.txt[/purple][red]"[/red] [red]][/red],  [gray][i]# Duplicate entry here (winvista64 in both 173 and 525).[/i][/gray]
[red]}[/red][red];[/red]

[gray][i]# This is simply an example operating system value.  I expect you'd get it from somewhere else.[/i][/gray]
[black][b]my[/b][/black] [blue]$operatingsystem[/blue] = [fuchsia]123[/fuchsia][red];[/red] [gray][i]# Unknown value to prove defaulting.[/i][/gray]

[gray][i]# Simply defining $Folder and $Readme with a restricted scope[/i][/gray]
[black][b]my[/b][/black] [red]([/red][blue]$Folder[/blue], [blue]$Readme[/blue][red])[/red][red];[/red]

[gray][i]# Very simple replacement for your entire if/else/elsif/elsif... structure[/i][/gray]
[olive][b]if[/b][/olive][red]([/red][blue]$operatingsystem[/blue] && [url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$os_lookup[/blue]->[red]{[/red][blue]$operatingsystem[/blue][red]}[/red][red])[/red] [red]{[/red]
    [gray][i]# Values from matched hash entry[/i][/gray]
    [red]([/red][blue]$Folder[/blue], [blue]$Readme[/blue][red])[/red] = [blue]@[/blue][red]{[/red][blue]$os_lookup[/blue]->[red]{[/red][blue]$operatingsystem[/blue][red]}[/red][red]}[/red][red];[/red]
[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
    [gray][i]# Default case[/i][/gray]
    [red]([/red][blue]$Folder[/blue], [blue]$Readme[/blue][red])[/red] = [red]([/red][red]"[/red][purple]win2k_xp[/purple][red]"[/red], [red]"[/red][purple]readme_2k_xp.txt[/purple][red]"[/red][red])[/red][red];[/red]
[red]}[/red]

[gray][i]# Just to show you what has been generated.[/i][/gray]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Folder: [[blue]$Folder[/blue]], Readme: [[blue]$Readme[/blue]][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

HTH



Trojan.
 
my boss is asking me to write in switch statement only please help me

Then you have to install and try the Switch module. But it is buggy. If you are using perl 5.10 you can use switch statements but they have a different syntax see my other post with a link. Thats all for me. Good luck with everything.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Trojan,
Thank you very much for your valuable time.i am debugging with new code.Thanks alot
 
HI Kevin,
Thank you verymuch for your suggestion.i am unable to find your switch statement syntax in other post?
 
Thanks Trojan
my perl version is v5.8.8
once again thank you for your GREAT Help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top