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

hello, i have the following in a text file: ip_address tab tab date where each word represents itself, as in the ip address could be 45.54.232.3 and the dates are all of the format 2002-31-01 i would like to create a search that given a date in the format 2002-31-01 and an ip adress 100.2.3.4 that will search the file and decide if the two are there on the same line.. i'm just having trouble with my matching.
my variables are $ip and $date what i want, but doesn't work: /($ip)(\t\t)($date)/ where \t are tabs, (do i need to include the tabs?
thanks, B

Replies are listed 'Best First'.
Re: simple search in a file
by sauoq (Abbot) on Jan 05, 2003 at 01:44 UTC

    That should work. Perhaps the lines don't all contain two tabs? Why not try something a little more robust like

    /$ip\s+$date/;
    instead. You really shouldn't need to capture anything with parens and you might consider using \Q and \E to quote the metachars in your variables otherwise the dots in your IPs will match any character at all.

    Also, make sure your variables hold what you expect. If you are reading them from a file, for instance, be sure they don't contain newlines.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: simple search in a file
by Paladin (Vicar) on Jan 05, 2003 at 00:43 UTC
    That RE works for me to match a line of the form you stated. Perhaps it's something else in your code that isn't right. Show us the code you have and maybe someone can spot the mistake.
Re: simple search in a file
by pfaut (Priest) on Jan 05, 2003 at 00:46 UTC

    If you know exactly what your record will look like, there's no reason to use pattern matching. You could build the record you're looking for and compare using 'eq'.

    Post the rest of your program here so we can see exactly what you're doing.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: simple search in a file
by osama (Scribe) on Jan 05, 2003 at 21:16 UTC
    here's what I would write:
    my $MATCH=0 while(<infile>) { if ($_ eq "$ip\t\t$date\n") {$MATCH=1; last;} }