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

Re^2: Regex matching part of one hash key to another

by KevinNC (Initiate)
on Jan 07, 2011 at 15:54 UTC ( [id://881098]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex matching part of one hash key to another
in thread Regex matching part of one hash key to another

Thanks for the tips. I am a very beginning perl user, I picked up the Llama book a month ago and have been with it off and on since. This is not my primary job function, I'm more of a NOC person but there was some free time and was given the go ahead to try and pick up some coding again to help reduce some manual processes, so most of the stuff is adaptations from the books examples and my, VERY, limited knowledge. It has been 18 years since I have written any code in school as an elective class. I'm hoping to use this more and more and become more effecient and these tips help out a lot. I have cleaned up the code based on the suggestions and have reposted here. Most of the time I use the explicits to help learn what is going on and make it clear to my n00b eyes :).

use 5.010; use strict; use warnings; # filehandle section open STDOUT, ">ouiresults.txt"; # Output file after program is ru +n. open STDERR, ">errlog.txt"; # Error Log. my $f1 = 'oui.txt'; # Filehandle of the OUI list from + the IETF used to create a hash of oui and company. my %hash = (); # Declared Hash to be created w +ith the input file to store the IP to MAC mappings. my %oui = (); # Delcared Hash containing the O +UI to Company mapping. open (LIST1, $f1) || die "File not found\n"; # Takes the oui +file and creates a hash while (<LIST1>) { # Key is OUI chomp; # Value is Company if (/\x20{5}\(base 16\)\t{2}/) { $oui{$`} = $'; } } close LIST1; open(INPUT, $ARGV[0]) || die "Cannot do eet! $!"; # Takes the inp +ut file name on the command line and builds a hash. while (<INPUT>) { # This is the ARP + Cache file created from a copy/paste of a Cisco routers arp cache if ($_ =~ m/(\d+\.\d+\.\d+\.\d+).*(\w{4})\.(\w{4})\.(\w{4})/) { + # Key is MAC my $temp = $2.$3.$4; + # Value is IP $hash{$temp} = $1; } } close(INPUT); while ( my ( $key, $value ) = each %hash ) { my $match = substr $key, 0, 6; say "$hash{$key} mapped to $key for company $oui{$match}!"; }

OUTPUT

10.147.23.11 mapped to 001320048fef for company Intel Corporate!

10.147.23.4 mapped to 08004ec802b4 for company !

10.147.23.1 mapped to 000b462d5869 for company !

INPUT

Internet 10.10.10.1 - 000b.462d.2846 ARPA FastEthernet0

Internet 10.10.10.2 1 0013.2004.acde ARPA FastEthernet0

Internet 10.10.10.3 7 0800.4ec8.94ac ARPA FastEthernet0

Thanks again for everyone's comments, I'm looking forward to more, I'm hooked!

Replies are listed 'Best First'.
Re^3: Regex matching part of one hash key to another
by KevinNC (Initiate) on Jan 07, 2011 at 17:22 UTC

    Also, Here is the parts of the oui.txt file that should match, sorry for not putting this up earlier.

    I should also state, for completeness, that im using Strawberry perl 5.12.0.1 for Windows on an XP platform and have no ability to get cpan modules from work due to the firewall/proxy rules. :(

    00-13-20 (hex) Intel Corporate 001320 (base 16) Intel Corporate Lot 8, Jalan Hi-tech 2/3 Kulim Hi-Tech Park Kulim Kedah 09000 MALAYSIA 00-0B-46 (hex) Cisco 000B46 (base 16) Cisco 80 West Tasman Dr. SJ-M/1 San Jose CA 95134 UNITED STATES 08-00-4E (hex) 3COM EUROPE LTD. 08004E (base 16) 3COM EUROPE LTD. 3COM CENTRE BOUNDARY WAY, HEMEL HEMPSTEAD HERTFORSHI UNITED KINGDOM UNITED

      Monks of Wisdom,

      So, after a bit of a break from this to do other work, I'm trying (once again) to resolve the previous issues and now I am seeing some warnings in my output. To my beginners eyes it appears there may a Case issue. I noticed the only difference between the output line that matches and the ones that fail is that the match portion are all numbers. Futhermore, when I checked the data files I noticed the input search strings are in lowercase and the oui strings to match against are in UPPERCASE. Currently using oko1's version of code (shown below) for simplicity. THANKS!
      #!/usr/bin/perl -w use 5.010; use strict; my %oui; open my $list1, "ouisample.txt" or die "ouisample.txt: $!\n"; while (<$list1>){ if (/^(.*?) {5}\(base 16\)\t\t(.*)$/) { $oui{$1} = $2; } } close $list1; while (<>){ if (/((?:\d+\.){3}\d+).*([\w.]{14})/i){ (my $temp = $2) =~ tr/.//d; print "$1 mapped to $temp for company ", $oui{substr $temp, 0, + 6}, "\n"; } }
      I updated one line and got it to work finally!
      (my $temp = uc($2)) =~ tr/.//d;
      Thanks a bunch, I'm so excited the script finally works as hoped.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found