You might do something like this:
#!/usr/bin/perl -w
use strict;
# good to see you are using -w and strict...way to go!
my $filename = "somefile";
# rename bit to found to be more descriptive
my $found = 0;
my $search = $ARGV[0];
# that should be $! ...you had !$ I suspect typo.
open(FILE,$filename) or die ("Couldn't open file - $!\n");
while (<FILE>) {
chomp;
if ( /^-/ ) { # $_ is the default for matches
$found = 0;
# you only want to test against the next line, so get it
my $line = <FILE>;
chomp $line;
if( $line =~ /^$search/o ) { # use the o modifier so it will only
compile the regex once (it never changes)...you also don't need the *, by
itself it will match like you want.
$found = 1;
}
}
if ($found) {
print "$_\n";
}
}
close(FILE);
----- Original Message -----
From: "Eric Plowe" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, July 26, 2002 9:31 PM
Subject: regex questions
> Hello all.
>
> I am working on a script that searches through a text file - each
> 'record' within the text file is separated by a - so for example:
>
> -
> Fancy Car
>
> text text
> text text
> -
>
> and so on and so on.
>
> the first initial row of text is what I am searching for - then I can
> handle parsing the rest of the text after that to the next delimiter.
>
> But right now with the script.. its really basic, you can pass a search
> argument to it and it'll return results. but I need to narrow it down.
> because it may return data I didnt search for. but it match to some
> extent the input. Here's the script as I have it now.
>
> --
> #!/usr/bin/perl -w
>
> use strict;
>
> my $filename = "somefile";
> my $bit = 0;
> my $search = $ARGV[0];
>
> open(FILE,$filename) or die ("Couldn't open file - !$\n");
>
> while (<FILE>) {
> chomp;
> if ($_ =~ /^-/) {
> $bit = 2;
> next;
> }
>
> if ($_ =~ /^$search*/) {
> $bit = 1;
> }
>
> if ($bit == 1) {
> print "$_\n";
> }
> }
> close(FILE);
> --
>
> the bit variables are there to basically do this: If the first line is
> a - (the record delimiter) then set $bit to 2 and scroll to the next
> line. If the line matches the inputed search - then set bit to 1 - so
> then now as it scrolls through the while, as long as $bit hasn't been
> changed to 2 - it'll print out each line until it hits the next '-'
>
> Now my question for the perl gurus is - How to optimize the regex (or
> lack of) I am using to match my inputed search? all I want to test
> against is the next line after a '-' and still attempt to match it even
> if its not the exact word (ie: typing in 'off' it would match off,
> office, Office, OFFICE, OFF, fell OFF the bike... i think you get my
> point) Thanks! and I hoped I explained my question/problem clearly.
>
> ~Eric
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]