Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > processing text

Reply
Thread Tools

processing text

 
 
George
Guest
Posts: n/a
 
      01-15-2009

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
<>
%%

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/
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      01-15-2009
>>>>> "G" == George <> writes:

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

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      01-15-2009
George wrote:

>
> 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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-15-2009
>>>>> "CM" == Chris Mattern <> writes:

CM> On 2009-01-15, Uri Guttman <> wrote:
>>>>>>> "G" == George <> writes:

>>

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)

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

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-15-2009
On Thu, 15 Jan 2009 15:16:02 -0500, Uri Guttman <> wrote:

>>>>>> "CM" == Chris Mattern <> writes:

>
> CM> On 2009-01-15, Uri Guttman <> wrote:
> >>>>>>> "G" == George <> writes:
> >>

> 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)
>
> 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
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-15-2009
On Thu, 15 Jan 2009 22:13:53 GMT, wrote:

>On Thu, 15 Jan 2009 15:16:02 -0500, Uri Guttman <> wrote:
>
>>>>>>> "CM" == Chris Mattern <> writes:

>>
>> CM> On 2009-01-15, Uri Guttman <> wrote:
>> >>>>>>> "G" == George <> writes:
>> >>

>> 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)
>>
>> 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


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
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      01-16-2009
On Thu, 15 Jan 2009 11:41:49 -0800, Tim Greer wrote:

> George wrote:
>
>>
>> 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.


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
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      01-16-2009
On Thu, 15 Jan 2009 14:19:33 -0500, Uri Guttman wrote:

>>>>>> "G" == George <> writes:

>
> 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 <>
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      01-16-2009
George wrote:

> 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).
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-16-2009

George wrote:
> 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)
>



--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
Processing pathnames listed in a text file. Jason Heyes C++ 4 03-24-2005 11:47 AM
Post-Processing RAW vs Post-Processing TIFF Mike Henley Digital Photography 42 01-30-2005 08:26 AM
Question: processing HTML, re-write default processing action of many tags Hubert Hung-Hsien Chang Python 2 09-17-2004 03:10 PM
"Text Processing in Python" review on Slashdot Joe Francia Python 0 07-08-2003 04:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57