Re: Find and delete a line, problems finding
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 8 May 2006 12:06:03 -0700
sakuragirl1 wrote:
I got my first PERL project last Monday
Then this is the time to start forming some good programming practices.
First of all, the language is called "Perl". The program that runs
your Perl scripts is called "perl". There is no "PERL".
and have written 3 pages of
working PERL, but 1 page is driving me nuts! I am very much a newbie,
so please have patience with me. below is a snippet of my code that is
having problems.
open(LISTFILE, "<.ccglist");
open(TEMPFILE, ">.tempfile");
1) Use lexical filehandles, rather than global barewords
2) Use the three-argument form of open
3) Always, yes, *always* check open() for success
open my $LISTFILE, '<', '.cgilist' or die "Can't open cgilist: $!";
open my $TEMPFILE, '>', '.tempfile' or die "Can't open tempfile: $!";
while ($line=<LISTFILE>) {
Always always always start your scripts with the lines
use strict;
use warnings;
right under the shebang. This will force you to declare your
variables, and warn you when you're doing things that are 95% likely to
be wrong....(such as below)
while (my $line = <$LISTFILE>) {
print TEMPFILE $line unless $line =~ (/$accountnumber/ &&
/$effectivedate/);
And here's a problem. If you had enabled warnings, Perl would have
told you that you're using an undefined value in the pattern matches.
Why? Because your && condition is wrong. You are testing whether or
not $line matches the return value of the and'ing of two patternmatches
against $_. You see, when you don't explicitly bind a pattern match to
a variable, it implicitly binds to $_. And that's what you did above,
twice. Your code is equivalent to:
.... unless $line =~ ( ( $_ =~ /$accountnumber/) && ($_ =~
/$effectivedate) );
whereas what you meant was:
.... unless $line =~ /$accountnumber/ && $line =~ /$effectivedate;
}
close TEMPFILE; close LISTFILE;
Put one statement per line. That way, if something goes wrong with one
of the statements, you will be able to tell which one it is, simply
from the error message that Perl gives you.
close $TEMPFILE;
close $LISTFILE;
unlink ".ccglist";
That's not the same filename that you opened above. Are you sure
that's what you meant? This is a good reason to declare a variable,
set it equal to the filename, and then use that variable throughout
your program. That way, 1) 'use strict;' will tell you when you make a
typo, and 2) your program is vastly easier to maintain should you need
to change the name of the file it uses.
rename ".tempfile", ".ccglist";
Again, check return values for *all* system calls:
unlink '.ccglist' or die "Couldn't delete .ccglist: $!";
rename '.tempfile', '.ccglist' or die "Couldn't move tempfile to
ccglist: $!";
I have finally got the deleting of a line working, but it is the
finding i am having a problem with. two problems really:
1) I need to search for a line that has the indicated account number
and effectivedate. I have found that this code will not take into
account the effective date.
Make the changes above, and this problem is solved.
2)I have tried without the effective date and it will find the
account number, but also finds any accounts that start with the account
number begining "pattern" as well. i need it find JUST the accounts
that match EXACTLY. e.g. user wants to delete account 123456, code
deletes 123456 AND 1234567.
You need to anchor your patterns to the start and/or end of the string.
Read
perldoc perlretut
to learn how to do this. At it's most basic, ^ means "start of string"
and $ means "end of string".
if ($line =~ /$number/) {
print "$line contains $number in it somewhere\n";
}
if ($line =~ /^$number/) {
print "$line *starts with* $number\n";
}
On another note, whenever you're trying to match against user-supplied
data, it's a good idea to escape the patterns, just in case the text
the user entered happens to contain characters that are special in
regular expressions:
if ($line =~ /^\Q$number\E/) { ... }
Read about that at
perldoc -f quotemeta
Thank you so much for any suggestions. I love working in PERL and hope
to have more time to study it, but I need to turn this project in ASAP.
This is, in general, a REALLY BAD THING to say in a newsgroup such as
this. Many people will take offense at it. Your (or your company's)
failure to plan or hire someone who already knows the language is not
sufficient reason for us to give you solutions in place of you learning
about the language. Indeed, had I seen this line before I pushed the
"reply" button, there's a good chance I wouldn't have responded at all.
Thanks again!
You're welcome.
Paul Lalli
.
- Follow-Ups:
- Re: Find and delete a line, problems finding
- From: Matt Garrish
- Re: Find and delete a line, problems finding
- References:
- Find and delete a line, problems finding
- From: sakuragirl1
- Find and delete a line, problems finding
- Prev by Date: FAQ 3.18 How can I free an array or hash so my program shrinks?
- Next by Date: Re: Disappointed by perl..
- Previous by thread: Find and delete a line, problems finding
- Next by thread: Re: Find and delete a line, problems finding
- Index(es):