String matching not working

D

Dave from Dublin

Hi,

I am quite new to Perl. I have several sets of data and within each
individual file news stories are seperated using ========.

My goal is to recreate each file without this divider. Eventually I
will look to iterate through the whole directory but for now I just
want to print all lines but the divider into a new file for one file
(1.ref)

But it's not working, all lines are being printed. Can anyone tell me
what's wrong (see code below), NOTE the ELSE is just a test
Thanks,
David


#!/usr/bin/perl

open(FILEREAD, "< 1.ref");
open(FILEWRITE, "> 1a.ref");
while (<FILEREAD>){
chop ( $line );
if ($line !=~ /====/ ){
print FILEWRITE;
}
else{
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
}
}
close FILEWRITE;
close FILEREAD;
 
E

Eric Amick

Hi,

I am quite new to Perl. I have several sets of data and within each
individual file news stories are seperated using ========.

My goal is to recreate each file without this divider. Eventually I
will look to iterate through the whole directory but for now I just
want to print all lines but the divider into a new file for one file
(1.ref)
#!/usr/bin/perl

open(FILEREAD, "< 1.ref");
open(FILEWRITE, "> 1a.ref");

Always check if open() has succeeded. Always.
while (<FILEREAD>){
chop ( $line );

Where did $line get a value? And chomp is safer.
if ($line !=~ /====/ ){

The operator you want is !~
 
S

Steve Grazzini

Dave from Dublin said:
But it's not working, all lines are being printed.

while (<FILEREAD>){
chop ( $line );
if ($line !=~ /====/ ){
print FILEWRITE;
}

Well, for starters you never put anything in $line.
Turning on 'warnings' would help (it will tell you
whenever you use an uninitialized variable).

And then the "doesn't match" operator is mispelled.

You should also prefer chomp() to chop(), but I don't
think you want either in this case:

while (<FILEREAD>) {
print FILEWRITE unless /====/;
}

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top