tophat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

Ok full disclosure, I am completely new to scripting. I am essentially trying to recreate Excel's vlookup function. I have a file with a list of names, and I am trying to use these names to match them to their IP addresses on a different file.To be clear, I have 2 files, one with user names and another with user names and IP addresses. The goal is to use a name and return their IP address From what I do understand it's a pattern matching problem but my string will vary depending on the user name. Any ideas how I can structure this script are welcome! Thank you for reading.

Edit: Update

Thank you all. Even writing this response is helping me solve this problem To clarify, I am not using excel files. I mentioned it, because it seemed to be the quickest way to describe what I am trying to do. But I am working with two txt files that are lists. Here is the snippet of code that has been working for me so far, the variable $flag1 is the string I used to generate my list of user names.I added the line iterator and the "xx" so I have a pattern to match if I needed it. Essentially this snippet creates my list of users, or if you will, the first file. I now wish to use this output and match it against my second file of user and IP addresses.

What I can do: match and return values for a known and fixed string pattern (in this case the string "failed for user [],"

What I am having trouble with: using my output in the first file as a pattern for finding results in the second file. Is this a foreach situation? Or perhaps I should do a while loop?

my $linenum2=1; open FORMATLOG, "<$outputfile" or die "Cannot open $outputfile $!\n"; open (REPORT, ">$badreport") or die "Cannot open $badreport $!\n"; my @line =<FORMATLOG>; print "lines that matched $flag1\n;"; for (@line) { if ($_=~ m/failed for user (.*?),/) { print REPORT $linenum2++; print REPORT " :$1xx\n"; } } close FORMATLOG; close REPORT;

Replies are listed 'Best First'.
Re: How to use matched strings
by mikeraz (Friar) on Apr 12, 2011 at 16:49 UTC

    Even completely new you must have some idea. How would you manually do it? Can you translate that into Perl?

    Please, let us see where you have run into a wall after attempting a solution.
    You'll learn a lot from the attempt - more than you will from reading solution suggestions here.


    Be Appropriate && Follow Your Curiosity
Re: How to use matched strings
by toolic (Bishop) on Apr 12, 2011 at 17:03 UTC
    One approach is to read the file with names and addresses into a Perl hash, with the name as a key and the address as a value. A simple hash requires that the names be unique.

    Then, you can read the names file and use each name as a key into your hash to get the address.

    Since you are new to Perl, a good free starting point is perlintro.

      Thank you, I am going to try this and report back.

        Just read your update. foreach? while? up to you, both could work here. One approach to your query:

        #!/usr/bin/perl use strict; use warnings; open my $CUR, "<", "tdat_cur" or die "no tdat_cur"; open my $IPS, "<", "tdat_ips" or die "no tdat_ips"; my @cur = map { chomp ; $_ } <$CUR>; my %ips; foreach ( <$IPS> ) { # if there's a chance of $nam recurring you need to check for it. my ($nam, $ip) = split; $ips{$nam} = $ip; } foreach my $curious_about ( @cur ) { if( defined $ips{$curious_about} ) { print "$curious_about has IP addr $ips{$curious_about}\n"; } else { print "No IP addr information for $curious_about\n"; } }
        tdat_cur contains: michael joe renee
        tdat_ips contains: rose 10.100.100.12 michael 10.100.100.13 renee 10.100.100.14 nizar 10.100.100.15

        As you can see I'm a foreach kinda guy. That data is very simplified and you'll probably need to handle a slightly more complex match and split.


        Be Appropriate && Follow Your Curiosity
Re: How to use matched strings
by young_david (Friar) on Apr 12, 2011 at 17:07 UTC

    Are the files you are reading formatted in Excel? If so, you may want to consider using the module Spreadsheet::ParseExcel

    It would be much more helpful to see any code you have written so far. Please post what you have.

Re: How to use matched strings
by dj_nitrkl (Initiate) on Apr 12, 2011 at 18:48 UTC
    can you use something like below for the purpose ? open (USERANDIP,"<userandip") or die "Cannot open ip file $!\n"; open ( USERNAME , "<username" ) or die "Cannot open user file $!\n"; @ip = <USERANDIP>; while (<USERNAME>){ $user = $_ ; @result = grep (/$user/,@ip); print "@result" ; }