need help with text wrap problem...

Discussion in 'Perl Misc' started by jac, Apr 20, 2006.

  1. jac

    jac Guest

    Ok, so far so good goes my first *productive* PERL program ever...

    This program opens a text file and prints everything except the lines
    containing words "adding", "unable", and "PNL". So far it mostly works
    like I want it to except that the lines of text that contain the word
    "unable" wrap at column "58" to a new line. I need to omit the new line

    as well as its part of the line that contains the word I don't want.
    Is there a pattern-match that I can use to delete a next line?


    ---INPUT TEXT -----------------------------------------------------|


    196315-2 CASTING,VOLUTE,8.5
    2-76811 WILL-WA PTAC02 XT70
    Unable to fetch part description for part 210-9--4-04120-
    210-9--4-04120-
    Unable to fetch part description for part 210-9--4-04120-
    210-9--4-04120-
    231146-76811 S.S. TAG-REF DESIG LABEL KIT
    231157-76811 S.S. TAG INSTALLATION KIT


    sample output -------------------------------------------------------|


    185650-76811-1 LABEL GATHERING KIT, DRVN, CENT
    196315-2 CASTING,VOLUTE,8.5
    2-76811 WILL-WA PTAC02 XT70
    210-9--4-04120-
    210-9--4-04120-
    231146-76811 S.S. TAG-REF DESIG LABEL KIT
    231157-76811 S.S. TAG INSTALLATION KIT


    ---------------------------------------------------------------------------­--|



    In the sample output, the numbers "210-9--4-04120-" are wrapped from a
    line above it that contained the word "unable"


    --- code example-----------------------------------------|


    open PULLIN,"$in" || die "$in does not exist: $!\n";
    open PULLOUT, ">output.txt" || die "$!\n";


    while (<PULLIN>) {
    if (! /adding|unable|PNL/i) {
    print PULLOUT;
    }


    }


    close(PULLIN);
    close(PULLOUT);

    --- end code -------------------------------------------------|


    Thanks,


    Alex
     
    jac, Apr 20, 2006
    #1
    1. Advertising

  2. jac wrote:
    > Ok, so far so good goes my first *productive* PERL program ever...
    >
    > This program opens a text file and prints everything except the lines
    > containing words "adding", "unable", and "PNL". So far it mostly works
    > like I want it to except that the lines of text that contain the word
    > "unable" wrap at column "58" to a new line. I need to omit the new
    > line


    So you want to skip the line that is after the line containing 'unable'?
    Just read it without processing it further.

    [...]
    > while (<PULLIN>) {
    > if (! /adding|unable|PNL/i) {
    > print PULLOUT;


    if (/Unable/) {undef = <PULLIN>} #read another line and discard it

    > }
    > }


    jue
     
    Jürgen Exner, Apr 20, 2006
    #2
    1. Advertising

  3. jac

    jac Guest

    ok -- good... See? this is part about processing files that confuses
    me. I'm still under the impression that PERL or any other language for
    that matter, treats a text file as a "glob" -- I don't know a better
    way to explain it.

    Does PERL process each line individually? and then considers the next
    line to be something new?

    the file--------------------------------------------------------
    one line (do something to this line)
    two lines (don't do anything to this line)
    three lines (do something else to this line)
    ------------------------------------------------------------------

    I guess I don't understand what makes a perl program treat each line
    individually.
     
    jac, Apr 20, 2006
    #3
  4. jac wrote:
    > ok -- good... See? this is part about processing files that confuses


    _WHAT_ is part about ....? Please quote appropriate context -as has been
    customary for decades- when replying such that people know what your are
    talking about.

    > me. I'm still under the impression that PERL or any other language for
    > that matter, treats a text file as a "glob" -- I don't know a better
    > way to explain it.


    It does. Sometimes. Depending on _how_ you are reading the file.

    > Does PERL process each line individually? and then considers the next
    > line to be something new?


    If you read the file line by line as in
    while (<FILE>) {....}
    or
    $line = <FILE>;
    then yes. Also in
    @all_lines = <FILE>;
    where each array element will contain one line of the file.

    If you read the file by other means e.g. using read() then no.

    > I guess I don't understand what makes a perl program treat each line
    > individually.


    It's the INPUT_RECORD_SEPARATOR variable "$/". Further details see perldoc
    perlvar.

    jue
     
    Jürgen Exner, Apr 20, 2006
    #4
  5. jac

    jac Guest

    jue,
    Thank you for the explanation. Unfortunately, the example you offered
    me didn't work for my application.

    >"if (/Unable/) {undef = <PULLIN>} #read another line and discard it"


    How about this?
    Is there a way to delete a line of text where the line wraps into the
    next line? Here's what I'm thinking about...

    -----
    thislinewrapsthislinewrapsthislinewrapsthislinewrapsthislinewraps
    thislinewrapsthislinewraps
    ----

    maybe something line "if charlen > 80; delete the next line;
    Does a _line wrap_ character exist in PERL? maybe I can look for that
    and delete it...

    I just dont' know what PERL syntax to use for this as I've only been
    studying PERL for a month and a half. I'm a web designer so I'm not
    even thinking like a software engineer. Maybe I need to -- I don't
    know.. thanks for your help so far...

    Alex
     
    jac, Apr 20, 2006
    #5
  6. jac

    cmic Guest

    Hello Jac,
    jac wrote :

    > jue,
    > Thank you for the explanation. Unfortunately, the example you offered
    > me didn't work for my application.
    >
    > >"if (/Unable/) {undef = <PULLIN>} #read another line and discard it"

    >
    > How about this?
    > Is there a way to delete a line of text where the line wraps into the
    > next line? Here's what I'm thinking about...


    If the line containing "Unable ..." *always* wraps, then you get the
    solution : delete every
    line after the line containing the "Unable" thing.

    But How come your imput file is wrapped ? Can you control the wrapping
    part of the process that yields you input file ? I think you'd better
    try this first.
    Regards
    --
    cmic SysAdmin
    >
    > -----
    > thislinewrapsthislinewrapsthislinewrapsthislinewrapsthislinewraps
    > thislinewrapsthislinewraps
    > ----
    >
    > maybe something line "if charlen > 80; delete the next line;
    > Does a _line wrap_ character exist in PERL? maybe I can look for that
    > and delete it...
    >
    > I just dont' know what PERL syntax to use for this as I've only been
    > studying PERL for a month and a half. I'm a web designer so I'm not
    > even thinking like a software engineer. Maybe I need to -- I don't
    > know.. thanks for your help so far...
    >
    > Alex
     
    cmic, Apr 20, 2006
    #6
  7. jac

    jac Guest

    >If the line containing "Unable ..." *always* wraps, then you get the solution : delete every line after the line > containing the "Unable" thing.

    agreed, but what does the syntax look like delete a line *after*?
    that's what I'm having a difficult time with.
     
    jac, Apr 20, 2006
    #7
  8. jac

    Xicheng Jia Guest

    jac wrote:
    > jue,
    > Thank you for the explanation. Unfortunately, the example you offered
    > me didn't work for my application.
    >
    > >"if (/Unable/) {undef = <PULLIN>} #read another line and discard it"

    >
    > How about this?
    > Is there a way to delete a line of text where the line wraps into the
    > next line? Here's what I'm thinking about...
    >
    > -----
    > thislinewrapsthislinewrapsthislinewrapsthislinewrapsthislinewraps
    > thislinewrapsthislinewraps
    > ----


    If you knew the exact position your lines are wrapped, like 58
    charaters, then you might try the following line to concatenate
    multiple wrapped lines:

    $_ .= <PULLIN>, redo unless ( (split//) % 57 );

    Sometimes there might exist some lines that have exactly 58 characters,
    then the next line should not be wrapped. You should have to find a way
    to phase these lines out. For example, in your case, you might need
    only lines containing a string 'Unable', then

    if ( /Unable/ ) {
    $_ .= <PULLIN>, redo unless ( (split//) % 57 );
    }

    which can concatenate all wrapped lines into one(in $_) and (at least)
    the first 58 charaters contain a string 'Unable'..

    If you do need the first 58 characters(ignore all the others), then
    use "substr($_,0,57)" to extract it after the 'if' statement..

    my 2 cents..

    Xicheng

    >
    > maybe something line "if charlen > 80; delete the next line;
    > Does a _line wrap_ character exist in PERL? maybe I can look for that
    > and delete it...
    >
    > I just dont' know what PERL syntax to use for this as I've only been
    > studying PERL for a month and a half. I'm a web designer so I'm not
    > even thinking like a software engineer. Maybe I need to -- I don't
    > know.. thanks for your help so far...
    >
    > Alex
     
    Xicheng Jia, Apr 20, 2006
    #8
  9. jac wrote:
    >
    > [snip]
    >
    > --- code example-----------------------------------------|
    >
    >
    > open PULLIN,"$in" || die "$in does not exist: $!\n";
    > open PULLOUT, ">output.txt" || die "$!\n";


    Nobody else has pointed this out but the only way that the first open will die
    is if $in contains '' or '0' and the second open will never die. The problem
    is that the precedence of the '||' operator binds it to the preceding
    expression and not to the open operator. You need to either use parentheses:

    open( PULLIN, $in ) || die "$in does not exist: $!\n";
    open( PULLOUT, ">output.txt" ) || die "$!\n";

    Or use the lower precedence 'or' operator:

    open PULLIN, $in or die "$in does not exist: $!\n";
    open PULLOUT, ">output.txt" or die "$!\n";




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 20, 2006
    #9
  10. jac

    Guest

    jac wrote:
    > A question


    This question was multi-posted to perl.beginners. The OP should realize
    that cross-posting is sometimes OK, but multi-posting is always a
    usenet mortal sin (extremely rude). Multi-posting is a good way to get
    killfiled.

    --
    http://DavidFilmer.com
     
    , Apr 20, 2006
    #10
  11. jac

    Guest

    try:

    $_=~ s#Unable to fetch part description for part (.*)#$1 Cannot fetch
    Description#gis;
     
    , Apr 21, 2006
    #11
  12. jac

    Ian Wilson Guest

    jac wrote:
    >
    > This program opens a text file and prints everything except the lines
    > containing words "adding", "unable", and "PNL". So far it mostly works
    > like I want it to except that the lines of text that contain the word
    > "unable" wrap at column "58" to a new line. I need to omit the new line
    >
    >
    > ---INPUT TEXT -----------------------------------------------------|
    > 196315-2 CASTING,VOLUTE,8.5
    > 2-76811 WILL-WA PTAC02 XT70
    > Unable to fetch part description for part 210-9--4-04120-
    > 210-9--4-04120-
    > Unable to fetch part description for part 210-9--4-04120-
    > 210-9--4-04120-
    > 231146-76811 S.S. TAG-REF DESIG LABEL KIT
    > 231157-76811 S.S. TAG INSTALLATION KIT
    >
    > sample output -------------------------------------------------------|
    > 185650-76811-1 LABEL GATHERING KIT, DRVN, CENT
    > 196315-2 CASTING,VOLUTE,8.5
    > 2-76811 WILL-WA PTAC02 XT70
    > 210-9--4-04120-
    > 210-9--4-04120-
    > 231146-76811 S.S. TAG-REF DESIG LABEL KIT
    > 231157-76811 S.S. TAG INSTALLATION KIT
    > ---------------------------------------------------------------------------­--|
    > In the sample output, the numbers "210-9--4-04120-" are wrapped from a
    > line above it that contained the word "unable"
    >


    Instead of discarding "bad" lines, it is sometimes easier to accept
    "good" lines:

    $ perl -n -e 'print if /^\d+-\d+\s+/' < t.txt
    196315-2 CASTING,VOLUTE,8.5
    2-76811 WILL-WA PTAC02 XT70
    231146-76811 S.S. TAG-REF DESIG LABEL KIT
    231157-76811 S.S. TAG INSTALLATION KIT

    YMMV.
     
    Ian Wilson, Apr 21, 2006
    #12
  13. jac <> wrote:

    > Does a _line wrap_ character exist in PERL?


    > I just dont' know what PERL syntax to use for this as I've only been
    > studying PERL for a month and a half.



    The name of the programming language is "Perl", not "PERL".


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 21, 2006
    #13
  14. jac

    Guest

    Jim Gibson <> writes:

    > In article <>, jac
    > <> wrote:
    >
    > > >If the line containing "Unable ..." *always* wraps, then you get the
    > > >solution : delete every line after the line > containing the "Unable"
    > > >thing.

    > >
    > > agreed, but what does the syntax look like delete a line *after*?
    > > that's what I'm having a difficult time with.

    >
    > Perl, like most procedural computer languages, does not modify files
    > directly,


    Perl can do read write access to the middle of a file.
    I would say that Perl is able to "modify a file directly".
    For example, a perl program can gzip files in place,
    even if the filesystem is too full to allow a copy of the file,
    by doing random access reads and implement write-behind,
    then truncate file when done. Easier and more practical
    (for non-enormous files) is to gzip to memory,
    then overwrite the shorter image to same filename.

    > so there is no 'delete line' command, and certainly no
    > 'delete line after x' command. A Perl program can read a file in and
    > write it contents out to the same file or another file. A Perl program
    > can overwrite a file or truncate a file at some point in the file,
    > deleting everything in the file after that point. Those are the basic
    > file manipulations, and the advice you have gotten so far assumes that
    > you knew this much.
    >
    > The way to delete a line from a file in Perl is to read the file,


    s/The way/A usual way/;

    > copying the contents to another file, but not copying the parts that
    > you want to delete. When you are done, you can use the new file as is
    > or copy it back to the old file.


    Or use -i switch to edit "in place"
    see perldoc perlrun, search for -i for details

    > So your program to 'delete line after' boils down to:
    >
    > 1. read a line from the old file
    > 2. write the line to the new file
    > 3. If the next line should be skipped, read a line from the old file
    > but do not write it to the new file.
    > 4. If there are more lines in the file, go to step 1.
    >
    > Lines in the file are determined by special characters that appear at
    > the end of each line.


    Ah, there it is, the definition of a line.
    If I find a <linemarker> character, and write over it with
    null or space, the modified file is the same length as the original,
    but has fewer lines. Does that count as "deleting a line"?
    A better description might be "unwrapping a wrapped line",
    which may (or may) not be what the OP wants.

    > The exact characters depends upon the platform
    > and file system you are using. Perl has several different ways of
    > reading files. See 'perldoc perlop' and search for 'I/O'.


    also helpful: perldoc -f open , search for '+<'

    --
    Joel
     
    , Apr 21, 2006
    #14
  15. jac

    Steve Swift Guest

    > This question was multi-posted to perl.beginners.

    What is the full name of the perl.beginners group, please? (So I can ask
    my ISP to carry it). (I searched for all groups containing "begin" but
    none of them also contained "perl"

    --
    Steve Swift
    http://www.ringers.org.uk
     
    Steve Swift, Apr 24, 2006
    #15
  16. jac

    Paul Lalli Guest

    Steve Swift wrote:
    > > This question was multi-posted to perl.beginners.

    >
    > What is the full name of the perl.beginners group, please?


    It *is* perl.beginners.

    http://www.nntp.perl.org/group/perl.beginners/

    > (So I can ask
    > my ISP to carry it). (I searched for all groups containing "begin" but
    > none of them also contained "perl"


    Try using the actual perl.org NNTP server, nntp.perl.org

    Paul Lalli
     
    Paul Lalli, Apr 24, 2006
    #16
  17. Steve Swift <> wrote:
    >> This question was multi-posted to perl.beginners.

    >
    > What is the full name of the perl.beginners group, please? (So I can ask
    > my ISP to carry it). (I searched for all groups containing "begin" but
    > none of them also contained "perl"



    It is not a Usenet newsgroup.

    It is a mailing list:

    http://www.nntp.perl.org/group/perl.beginners


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 25, 2006
    #17
  18. jac

    Dave Weaver Guest

    Xicheng Jia <> wrote:
    >
    > $_ .= <PULLIN>, redo unless ( (split//) % 57 );
    >

    --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^

    You mis-spelled 'if length($_) == 57'

    :)
     
    Dave Weaver, Apr 25, 2006
    #18
    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. Sara T.

    WRAP text in <TD>

    Sara T., Jul 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    643
    Sara T.
    Jul 26, 2003
  2. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    709
    Chronic Philharmonic
    May 10, 2008
  3. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    237
    Art Werschulz
    Sep 22, 2003
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    251
    Anno Siegel
    Sep 25, 2003
  5. Sunny

    Text wrap problem firefox

    Sunny, Oct 2, 2008, in forum: Javascript
    Replies:
    3
    Views:
    110
Loading...

Share This Page