Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Short and easy way to write if...elsif syntax

by grizzley (Chaplain)
on Aug 27, 2012 at 09:55 UTC ( #989957=note: print w/ replies, xml ) Need Help??


in reply to Short and easy way to write if...elsif syntax

Somehow funny way:

%versions= ( 'linux' => sub { print "This is linux VM\n" }, 'Windows' => sub { print "This is a Windows VM\n" } ); # create this list once before loop my $idlist = join "|", keys %versions; @array = ('linux', 'Windows', 'OSX'); foreach(@array) { if(/$idlist/) { my $id = $&; &{$versions{$id}}(); } else { print "I have no idea what it is\n"; } }

Update: please be careful with this approach, as you will have problems if one sysname is included in another one, e.g. 'Windows' and 'Windows 7' (well, actually this is a problem in your original if / elsif list, too.)


Comment on Re: Short and easy way to write if...elsif syntax
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (19)
As of 2015-07-06 13:48 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 (75 votes), past polls