in reply to Maintainable and robust Pattern Matching with changing goalposts

How about something like this for the Manufacturer list?

my(%manufacturers)= ( Cisco => qr/Cisco/i, 'TUT Systems'=> qr/Expresso GS|MDU Lite/i, Gigalink => qr/Gigalink/i, Huawei => qr/Huawei/i, Orinoco => qr/AP-2000|AP-1000/i, Lucent => qr/WavePOINT/i, HP => qr/HP/, Paradyne => qr/Paradyne/i, Colubris => qr/CN320/, SMC => qr/TigerSwitch/, Ricoh => qr/RICOH/, ); $ref->{$host}{$ip}{'manufacturer'}='*unknown*'; while (($manu, $re)= each(%manufacturers)) { if (/$re/) { $ref->{$host}{$ip}{'manufacturer'}=$manu; last; } }
This only works if the sequence of the checks is unimportant. If the sequence does matter, I'd change that to an array:
my(@manufacturers)= ( Cisco => qr/Cisco/i, 'TUT Systems'=> qr/Expresso GS|MDU Lite/i, Gigalink => qr/Gigalink/i, Huawei => qr/Huawei/i, Orinoco => qr/AP-2000|AP-1000/i, Lucent => qr/WavePOINT/i, HP => qr/HP/, Paradyne => qr/Paradyne/i, Colubris => qr/CN320/, SMC => qr/TigerSwitch/, Ricoh => qr/RICOH/, ); $ref->{$host}{$ip}{'manufacturer'}='*unknown*'; $manu= undef; foreach $re (@manufacturers) { if (defined $manu) { if (/$re/) { $ref->{$host}{$ip}{'manufacturer'}=$manu; last; } $manu= undef; } else { $manu= $re; } } print $m;

$\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

Replies are listed 'Best First'.
Re^2: Maintainable and robust Pattern Matching with changing goalposts
by Skeeve (Parson) on Sep 16, 2005 at 19:41 UTC
    To enhance my previous code a bit. One could also add regular expressions for finding the version etc. from the string like this (example given just for Cisco)

    my(%manufacturers)= ( Cisco => { key => qr/Cisco/i, version => qr/Internetwork.*(C[0-9]+[a-zA-Z]*?)|Cisco\s+(\d+)| +Cisco\s+(AP\w+)/, # the re will pick any possible Version number in $1.. $n }, 'TUT Systems'=> { key => qr/Expresso GS|MDU Lite/i, version => qr//, }, Gigalink => { key => qr/Gigalink/i, version => qr//, }, Huawei => { key => qr/Huawei/i, version => qr//, }, Orinoco => { key => qr/AP-2000|AP-1000/i, version => qr//, }, Lucent => { key => qr/WavePOINT/i, version => qr//, }, HP => { key => qr/HP/, version => qr//, }, Paradyne => { key => qr/Paradyne/i, version => qr//, }, Colubris => { key => qr/CN320/, version => qr//, }, SMC => { key => qr/TigerSwitch/, version => qr//, }, Ricoh => { key => qr/RICOH/, version => qr//, }, ); $_='Sysdescr:SNMPv2-MIB::sysDescr.0 = STRING: Cisco Internetwork Opera +ting System Software IOS (tm) C2900XL Software (C2900XL-C3H2S-M), Ve +rsion 12.0(5)WC9a, RELEASE SOFTWARE (fc1) Copyright (c) 1986-2004 by +cisco Systems, Inc. Compiled Tue 13-Jan-04 11:54 by antonino'; $m=undef; $manu= undef; $hash= undef; while (($manu, $hash)= each(%manufacturers)) { # code as before except that we get the "key"-re from a hash if (/$hash->{'key'}/) { $m=$manu; last; } $hash= undef; } if (defined $hash) { no warnings qw/uninitialized/; # if we found a hash we execute the version re $version= join('', /$hash->{'version'}/); # and join all the version numbers found - which is at most 1 ;) } print <<RESULT; $m $version RESULT

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print