In article <00e201c28426$051681e0$[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Patrick Salmon) writes:
>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.=20
>
>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) =3D Windows95
>    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   =20
>    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 =20
>    if (/^\srib/){s/A/Non-MS/;$legacy++;print DFILE;next;};   #   SNMP =
>Host =20
>    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=20
>    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 "?".

Sure there's no print DFILE in that last case?  Why the substitution then?

I'd do something like

my @rules = (
    qr/^\s[eE|aA]\d\d\d\d\d\D/   => sub { s/A/W95/;   $w95++  },
    qr/^\s[a-zA-Z]{5}dfs\d\d\d+/ => sub { s/A/W2K-S/; $w2ks++;},
  ...
);

OUTER: while (<SOMEINPUTSTREAM>) {
  for (my $i = 0; $i <= $#rules; $i += 2) {
    /$rules[$i]/ and do { $rules[$i+1]->(); print DFILE; next OUTER };
  }
  s/A/?/;
}


If it weren't for the fact that sometimes you increment two variables I'd
have used a hash for the increments instead.  Might still be better to have
a hash instead of all those little variables.

-- 
Peter Scott
http://www.perldebugged.com

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

Reply via email to