[EMAIL PROTECTED] wrote:
> Hi, list!
>  I know this is OT, but the Win32 list is ignoring my attempts to
> subscribe... 
> 
> I'm working with Win32_VideoController, trying to get the
> VideoArchitecture method. Unfortunately, that's one of several
> methods that returns an unsigned integer (16 bit, in this case).
> Here's my question:  How can I manipulate a uint16 (or a uint32, for
> that matter) so that it will work as the value in a "switch"
> statement? That is...     
> 
> for ($obj->VideoArchitecture) {
>    /^(1)$/ && do { $arch = "Other", last };
>    /^(2)$/ && do { $arch = "Unknown", last };
>    /^(3)$/ && do { $arch = "CGA", last };
>    . . .
>    $arch = "Whoopsie!" ; # DEFAULT
> }

Why the regex, and why the brackets if you don't use what they capture?
If the values are numeric then the following would probably be better:

        $_ == 1 && do ...

Also, '$obj->VideoArchitecture' doesn't look much like an array (given
your 'if' statement below), so why the 'for' loop?

> 
> When I try it, it doesn't even trip on the default!  However, it does
> work in the clumsy if-elsif-elsif-...-else structure: 
> 
> if ($obj->VideoArchitecture == 1) {
>    $arch = "Other";
> } elsif ($obj->VideoArchitecture == 2) {
>    $arch = "Unknown";
> } ...
> 
> I'd just prefer getting the more elegant code working (mostly 'cuz
> it's one of my favorite Perl thingies). 

A more 'perl-ish' way of doing this sort of thing would be to use a
hash, e.g.

my %arch_names = (1 => "Other", 2 => "Unknown", 3 => "CGA", ...);
$arch = $arch_name{$obj->VideoArchitecture};
$arch = "Whoopsie!" unless defined $arch;

Alternatively, if the values that you want to translate are all numeric,
and essentially contiguous, you could use an array.

However, if you really want to do it with a 'switch' statement you could
start by:

'ppm install Switch' if it isn't already installed, then
'perldoc Switch'

HTH

-- 
Brian Raven
 


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to