processing text

G

George

I thought I was past trouble working with simple file manipulations, but I
seem to be stumped here again:


#!/usr/bin/perl
use strict;
use warnings;

my $filename = 'larry1.txt';
my $outfile = 'processed1.txt'
open my $fh, '<', $filename or die "cannot open $filename: $!";
open my $gh, '>', $outfile or die "cannot open $filename: $!";
while (<$fh>) {
s/%%/%\n/;
print $gh, $_;
}
close($fh)
close($gh)

# perl larry1.pl


C:\MinGW\source> perl larry1.pl
"my" variable $filename masks earlier declaration in same scope at
larry1.pl lin
e 7.
"my" variable $filename masks earlier declaration in same statement at
larry1.pl
line 7.
syntax error at larry1.pl line 7, near "open "
Can't use global $! in "my" at larry1.pl line 7, near "$filename: $!"
syntax error at larry1.pl line 14, near ")
close"
Execution of larry1.pl aborted due to compilation errors.

C:\MinGW\source>

larry1.txt is 61 k of Larry Wall quotes, delimited by %% :

%%
if (instr(buf,sys_errlist[errno])) /* you don't see this */
-- Larry Wall in eval.c from the perl source code
%%
if (rsfp = mypopen("/bin/mail root","w")) { /* heh, heh */
-- Larry Wall in perl.c from the perl source code
%%
If you consistently take an antagonistic approach, however, people are
going to start thinking you're from New York. :)
-- Larry Wall to Dan Bernstein in
<[email protected]>
%%

I wanted to use them as a randomsig for dialog, which wants a single %
between quotes.

I tried the open statements a few different ways and get essentially the
same complaints from perl.exe.:-(

Thanks for your comment.

--
George

To those of you who received honours, awards and distinctions, I say well
done. And to the C students, I say you, too, can be president of the United
States.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
U

Uri Guttman

G> I thought I was past trouble working with simple file manipulations, but I
G> seem to be stumped here again:


G> #!/usr/bin/perl
G> use strict;
G> use warnings;

G> my $filename = 'larry1.txt';
G> my $outfile = 'processed1.txt'

missing ;

that causes that and the next line to be a single long statement which
is parsed wierdly and spits out lots of error.

G> open my $fh, '<', $filename or die "cannot open $filename: $!";
G> open my $gh, '>', $outfile or die "cannot open $filename: $!";
G> while (<$fh>) {
G> s/%%/%\n/;
G> print $gh, $_;

the file handle arg to print doesn't get a comma afterwards

G> }
G> close($fh)
G> close($gh)

uri
 
T

Tim Greer

George said:
I thought I was past trouble working with simple file manipulations,
but I seem to be stumped here again:


#!/usr/bin/perl
use strict;
use warnings;

my $filename = 'larry1.txt';
my $outfile = 'processed1.txt'

You forgot a semicolon there. = 'processed1.txt';
open my $fh, '<', $filename or die "cannot open $filename: $!";
open my $gh, '>', $outfile or die "cannot open $filename: $!";
while (<$fh>) {
s/%%/%\n/;
print $gh, $_;

print $gh $_; # no comma.
}
close($fh)

You forgot ;
close($gh)

You don't need a semi-colon on the last close statement, since that's
the end of the script, but you should probably add one anyway,
especially if code could follow.

You also might want to add a warning if the filehandle fails to close,
if it's anything important.
 
U

Uri Guttman

G> open my $fh, '<', $filename or die "cannot open $filename: $!";
G> open my $gh, '>', $outfile or die "cannot open $filename: $!";
G> while (<$fh>) {
G> s/%%/%\n/;
G> print $gh, $_;G> }
G> close($fh)
G> close($gh)

CM> More missing semicolons here that gave him the errors on the close
CM> statements.

and no one mentioned this is a trivial one liner (untested):

perl -ne 's/%%/%\n/' <infile >outfile

i don't know the file format so maybe a /g is needed there too.

uri
 
S

sln

G> open my $fh, '<', $filename or die "cannot open $filename: $!";
G> open my $gh, '>', $outfile or die "cannot open $filename: $!";
G> while (<$fh>) {
G> s/%%/%\n/;
G> print $gh, $_;
G> }
G> close($fh)
G> close($gh)

CM> More missing semicolons here that gave him the errors on the close
CM> statements.

and no one mentioned this is a trivial one liner (untested):

perl -ne 's/%%/%\n/' <infile >outfile

i don't know the file format so maybe a /g is needed there too.

uri
Its all that typing in 1 linears, over and over and over again.

sln
 
S

sln

Its all that typing in 1 linears, over and over and over again.

sln

Why don't you make a batch file, then you only need to type the batch file
name and parameters over and over and over and over and over again.
Still more lines. Another line for that line? another line, why not...

sln
 
G

George

You forgot a semicolon there. = 'processed1.txt';


print $gh $_; # no comma.


You forgot ;


You don't need a semi-colon on the last close statement, since that's
the end of the script, but you should probably add one anyway,
especially if code could follow.

You also might want to add a warning if the filehandle fails to close,
if it's anything important.

Thanks all for responses. This works famously:

#!/usr/bin/perl
use strict;
use warnings;

my $filename = 'larry1.txt';
my $outfile = 'processed1.txt';
open my $fh, '<', $filename or die "cannot open $filename: $!";
open my $gh, '>', $outfile or die "cannot open $filename: $!";
while (<$fh>) {
s/%%/%\n/;
print $gh $_;
}
close($fh);
close($gh);

# perl larry1.pl

When I give it to dialog to use for a randomsig, it works (see below) I
think I'll use this sig for c.l.p.misc for its pedagogical value. My days
as george are numbered (thank goodness). I think I'll remove some of the
newlines, so the sig is less than 4 lines like it should be.

Now I'm gonna try text processing with a more difficult data set.
--
George


if (instr(buf,sys_errlist[errno])) /* you don't see this */
-- Larry Wall in eval.c from the perl source code
 
G

George

G> I thought I was past trouble working with simple file manipulations, but I
G> seem to be stumped here again:


G> #!/usr/bin/perl
G> use strict;
G> use warnings;

G> my $filename = 'larry1.txt';
G> my $outfile = 'processed1.txt'

missing ;

that causes that and the next line to be a single long statement which
is parsed wierdly and spits out lots of error.

G> open my $fh, '<', $filename or die "cannot open $filename: $!";
G> open my $gh, '>', $outfile or die "cannot open $filename: $!";
G> while (<$fh>) {
G> s/%%/%\n/;
G> print $gh, $_;

the file handle arg to print doesn't get a comma afterwards

G> }
G> close($fh)
G> close($gh)

uri

Thx uri.

I looked back over this error message, and it doesn't "look like" I failed
to use a ;. I guess one of the things you always have to look for is if
you get an error on line 7, go look to see if line 6 is terminated
properly.

I have to admit that I don't understand at least half of these quotes.
--
George


Maybe we should take a clue from FTP and put in an option like "print
hash marks on every 1024 iterations". :)
-- Larry Wall in <[email protected]>
 
T

Tim Greer

George said:
My days
as george are numbered (thank goodness).

Thank God! The 20th is my birthday and I couldn't think of a better
present (I'm not being sarcastic).
 
R

RedGrittyBrick

George said:
I thought I was past trouble working with simple file manipulations, but I
seem to be stumped here again:


#!/usr/bin/perl
use strict;
use warnings;

my $filename = 'larry1.txt';
my $outfile = 'processed1.txt'
open my $fh, '<', $filename or die "cannot open $filename: $!";
open my $gh, '>', $outfile or die "cannot open $filename: $!";

^^^^^^^^ ^^^^^^^^^^
 
T

Tim Greer

RedGrittyBrick said:
^^^^^^^^                     ^^^^^^^^^^

Wow, I didn't even see that when I looked at his code. Good eye!

PS: To the OP, in case you didn't notice, you're reporting $filename
fails to open, when you're trying to open $outfile.
 
L

larry

Thank God! The 20th is my birthday and I couldn't think of a better
present (I'm not being sarcastic).

Thanks for your attention, Tim.

I have a much more difficult text processing project, furthermore, I am
switching nyms from the least curious george I ever witnessed. The only
gesture he left out of his last address was throwing out a nice piece of
poop for us all to share.

My identity will be Larry Gates, and my e-mail will be
(e-mail address removed) . If you're inclined to have me in your killfile,
*please* update this tradition.

I'll start a new thread with the name I wrote on the wall.
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top