processing text

Discussion in 'Perl Misc' started by George, Jan 15, 2009.

  1. George

    George Guest

    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/
    George, Jan 15, 2009
    #1
    1. Advertising

  2. George

    Uri Guttman Guest

    >>>>> "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 ---------
    Uri Guttman, Jan 15, 2009
    #2
    1. Advertising

  3. George

    Tim Greer Guest

    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!
    Tim Greer, Jan 15, 2009
    #3
  4. George

    Uri Guttman Guest

    >>>>> "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 ---------
    Uri Guttman, Jan 15, 2009
    #4
  5. George

    Guest

    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
    , Jan 15, 2009
    #5
  6. George

    Guest

    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
    , Jan 15, 2009
    #6
  7. George

    George Guest

    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
    George, Jan 16, 2009
    #7
  8. George

    George Guest

    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 <>
    George, Jan 16, 2009
    #8
  9. George

    Tim Greer Guest

    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!
    Tim Greer, Jan 16, 2009
    #9
  10. 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
    RedGrittyBrick, Jan 16, 2009
    #10
  11. George

    Tim Greer Guest

    RedGrittyBrick wrote:

    >>open my $gh, '>', $outfile or die "cannot open $filename: $!";

    >
    > ^^^^^^^^                     ^^^^^^^^^^


    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.
    --
    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!
    Tim Greer, Jan 16, 2009
    #11
  12. George wrote:
    > On Thu, 15 Jan 2009 11:41:49 -0800, Tim Greer wrote:
    >
    >> George wrote:
    >>

    > open my $gh, '>', $outfile or die "cannot open $filename: $!";


    if this line fails, you can't open $outfile, not $filename.
    Brian Helterline, Jan 16, 2009
    #12
  13. George

    Guest

    On Thu, 15 Jan 2009 17:44:29 -0800, Tim Greer wrote:

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


    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
    d . 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.
    --
    George

    It's hard to tune heavily tuned code. :)
    -- Larry Wall in <>
    , Jan 17, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes
    Replies:
    4
    Views:
    379
    Karl Heinz Buchegger
    Mar 24, 2005
  2. Joe Francia
    Replies:
    0
    Views:
    298
    Joe Francia
    Jul 8, 2003
  3. phil hunt

    Text-to-HTML processing program

    phil hunt, Jan 3, 2004, in forum: Python
    Replies:
    11
    Views:
    586
    Reinier Post
    Jan 8, 2004
  4. Michael Ellis

    Cleaner idiom for text processing?

    Michael Ellis, May 26, 2004, in forum: Python
    Replies:
    16
    Views:
    490
    Peter Otten
    May 27, 2004
  5. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    451
    Michael Foord
    Sep 17, 2004
Loading...

Share This Page