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

I need help badly.... I had a program to retrieve information from the datafile unfortunatelly, my program did not run as I expected. I had a datafile.txt with the following information:
User Name UserID Phone# I am looking for a program something to match User Name with the USer +ID, if the User Name and UserId are matched then, it print out the ph +one#. My program at this time is not mathing the user Name and USer ID but m +atching all including the Phone#. So when I put User Name = myname an +d USerID=1, the output is all the phone # that had a number 1, and th +e UserId that had a 1 #. Please, if anyone have a simple program like this ..please help. I wou +ld appreciate your help. Thanks

Replies are listed 'Best First'.
Re: Matching 2 Keywords to retreve information
by runrig (Abbot) on Sep 19, 2001 at 20:48 UTC
    What code do you have so far? If its not too long (and it shouldn't be for this homework problem, then go ahead and post it.
Re: Matching 2 Keywords to retreve information
by jj808 (Hermit) on Sep 19, 2001 at 20:48 UTC
    You could either create a regular expression to match both parts at the same time, or alternatively use the Perl grep function to match the username and userid individually.

    Using the following data file (phonebook.txt):

    Jon:1:0123456789 Rob 1 0987654321 Sean 4 075312468 Tom-3-0111 222 333 Colin 3 0222 111 5555
    You could write something like
    #! /usr/bin/perl my ($username,$userid) = @ARGV; undef @ARGV; my @phonebook = (<>); @phonebook = grep /\b$username\b/ , @phonebook; @phonebook = grep /\b$userid\b/ , @phonebook; print foreach(@phonebook);
    Then if you type
    cat phonebook.txt | match.pl Jon 1
    You will get Jon:1:0123456789 Hope this helps,

    JJ

    Update:
    I do realise that the above code still has bugs, I'm just trying to give our brother Monk some pointers. For a more robust solution, you should really define a file format (e.g. colon-sperated values), then you could use split to ensure you are only matching against the right fields.