Patrick Salmon wrote:
> 
> There's always another way to do it, right?
> 
> I have a piece of code that does repeated searches through an input string and
> modifies it according to certain criteria. What I have works great, but I'd like
> to know how to write it better/more efficiently.
> 
> Given that I'm going over the same record until I get a hit (or not), how would
> I re-write the following so that it's not a series of if ....if...if...if?
> 
>     if (/^\s[eE|aA]\d\d\d\d\d\D/){s/A/W95/;$w95++;print DFILE;next;}; #   (E or A + 
>nnnnn) = Windows95

[eE|aA] is NOT E or A, it is 'e' or 'E' or '|' or 'a' or 'A'.


>     if (/^\s[a-zA-Z]{5}dfs\d\d\d+/){s/A/W2K-S/;$w2ks++;print DFILE;next;}; #   W2K 
>Server
>     if (/^\s[a-zA-Z]{3}salnm+/){s/A/W2K-S/;$w2ks++;print DFILE;next;}; #   W2K Server
>     if (/^\s[a-zA-Z]{3}scadc+/){s/A/W2K-DC/;$dc++;print DFILE;next;}; #   w2k Domain 
>Controller
>     if (/^\s\w\w\wnfris\d\d\d/){s/A/Snap/;$snaps++;print DFILE;next;}; #   Quantum 
>Snap server
>     if (/^\susu/){s/A/Non-MS/;$switch++;print DFILE;next;};   #   Switch
>     if (/^\sus/){s/A/Non-MS/;$legacy++;print DFILE;next;};   #   Legacy device 
>(Print Server, etc)
>     if (/^\ssap/){s/A/Non-MS/;$legacy++;print DFILE;next;};   #   SAP Host
>     if (/^\srib/){s/A/Non-MS/;$legacy++;print DFILE;next;};   #   SNMP Host
>     if (/^\s\wdcs+/){s/A/DCD/;$legacy++;print DFILE;next;};    #   Data Center Device
>     if (/^\s[a-zA-Z]{3}de\d\d\d\d\d/){s/A/W2K/;$w2k++;$good++;print DFILE;next;};#   
>w2k desktop - obeying convention
>     if (/^\s[a-zA-Z]{3}le\d\d\d\d\d/){s/A/W2K/;$w2k++;$good++;print DFILE;next;};#   
>w2k laptop - obeying convention
>     if (/^\s[a-zA-Z]{3}d\d{1,6}/){s/A/W2K/;$w2k++;print DFILE;next;}; #   w2k 
>desktop - Pre-Nextwave
>     if (/^\s[a-zA-Z]{3}l\d{1,6}/){s/A/W2K/;$w2k++;print DFILE;next;}; #   w2k laptop 
>- Pre-Nextwave
>     if (/^\s[a-zA-Z]{3}l+/){s/A/W2K?/;$w2k++;print DFILE;next;}; #   w2k laptop - 
>probable.
>     if (/^\s[a-zA-Z]{3}d+/){s/A/W2K?/;$w2k++;print DFILE;next;}; #   w2k desktop - 
>probable.
> 
>     {s/A/?/;} # Anything left over is interesting. Flag it for follow-up with a "?".


You can factor out the common statements to make it shorter.

    if    ( /^\s[eEaA]\d{4}\D/ )          { s/A/W95/;    $w95++ }
    elsif ( /^\s[a-zA-Z]{5}dfs\d{3,}/ )   { s/A/W2K-S/;  $w2ks++ }
    elsif ( /^\s[a-zA-Z]{3}salnm+/ )      { s/A/W2K-S/;  $w2ks++ }
    elsif ( /^\s[a-zA-Z]{3}scadc+/ )      { s/A/W2K-DC/; $dc++ }
    elsif ( /^\s\w\w\wnfris\d\d\d/ )      { s/A/Snap/;   $snaps++ }
    elsif ( /^\susu/ )                    { s/A/Non-MS/; $switch++ }
    elsif ( /^\s(?:us|sap|rib)/ )         { s/A/Non-MS/; $legacy++ }
    elsif ( /^\s\wdcs+/ )                 { s/A/DCD/;    $legacy++ }
    elsif ( /^\s[a-zA-Z]{3}[dl]e\d{5}/ )  { s/A/W2K/;    $w2k++; $good++ }
    elsif ( /^\s[a-zA-Z]{3}[dl]\d{1,6}/ ) { s/A/W2K/;    $w2k++ }
    elsif ( /^\s[a-zA-Z]{3}[dl]+/ )       { s/A/W2K?/;   $w2k++ }
    else                                  { s/A/?/ }

    print DFILE;




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to