Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex problems

Status
Not open for further replies.

cyphrix

Programmer
Nov 16, 2006
27
US
Hi guys. I'm very new to Perl. Making the switch from Python (cringe). The reason is because Perl seems to have much better regex capabilities than Python does and I need to really get started on this data extraction script my boss wants. However, I am finding it hard to do something that should be extremely simple. All I want to do is have the script open a simple text file, read it and as its reading do pattern matching. I can't seem to get it to open, read, and scan though. This was quite simple in (gulp) Python...please help

$file = open(INFILE, 'document.txt') or die;
if($file =~ /[+|-]user ID/)
{print "found User ID"}
else
{print "User ID not found"

Just something as simple as that...the code runs (using Perl Express IDE nice prog by the way) but it always returns false but I know for a fact that the pattern is a working pattern and I know that there are user id's in the text file. Any help is appreciated.
 
Your "open" call isn't doing quite what you expect it to.

It opens the file "document.txt" for reading - so far so good. This creates a filehandle called INFILE, which is what you'll have to use to read contents from the file. The return value (which you're storing in $file) is merely true or false depending on whether your open was successful or not. Specifically, it doesn't put the contents of the file into $file.

For that reason, applying a regexp to $file won't do anything useful - you still need to read the contents of the file.

Normally, you'd try to avoid reading in the whole file at once (to save memory) and do *something* like this (depending on your requirements obviously):
Code:
open(INFILE, 'document.txt') or die;
while(<INFILE>) {
   if( /[+|-]user ID/ ) {
      print "found User ID";
      last;
   }
   elsif ( eof ) {
      print "User ID not found";
   }
}
close INFILE;

Of course you still can slurp the file into a single scalar if you really want to:
Code:
open(INFILE, 'document.txt') or die;
local $/;
my $file = <INFILE>;
if($file =~ /[+|-]user ID/) {
    print "found User ID";
}
else {
   print "User ID not found";
}
close INFILE;
 
Code:
use strict;
use warnings;

open(INFILE, 'document.txt') or die $!;

while (<INFILE>) {
   print if /[+|-]user ID/;
}

close(INFILE);
As you have coded it the regex will match on "+user ID", "|user ID", or "-user ID" anywhere in the line. Is that what you want?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks very much to both of you for your help. I was able to get the file opened and read perfectly.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top