Modifying a HP/GL2 output file using Perl

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Modifying a HP/GL2 output file using Perl

    Hi gurus,

    I read some docs about Perl and I think it would be the best language to
    suit my acutal need.

    I have to change plotter pen widths from an HP/GL2 output file with specific
    widths. An HP/GL2 is a plain-text file filled with plotter commands. The
    one I need to check are :
    PSx (where x = pen number) = PEN SELECT
    PWx (where x = width) = PEN WIDTH

    In that kind of file, you will see PS1 followed by a bunch of coordinates
    and stuff. From there you can see that every command after the PS1 is for
    the PEN NUMBER ONE. I have to check where the PW is, and affect a new width
    right after it. I have to do this to all pens. I already know which width
    goes for each pen. My problem is that the number of caracters between each
    PSx and PWx are different from time to time.

    Unless anyone has a better idea it would probably need to read the file byte
    per byte, and when a PSx is read, the program will remember the current
    pen, and when reaching the next PWx, it will affect the corresponding width
    for the current pen.

    Is there an quick and effective way to do such kind of things with Perl?
    From what I've read, I can make a Perl script read a file LINE by LINE but
    I don't know about reading byte per byte.

    Any ideas?

    Thanks in advance,

    Steve
    Steve Hemond Guest

  2. Similar Questions and Discussions

    1. getting output of telnet from perl
      hi i had written code to connect to unix server from Windows xp(telnet) where i installed perl. i have to do with telnet. I think I am able to...
    2. modifying perl files with coldfuion
      i have a perl script which i got from a friend (i dotn really know much about perl). i use the script a lot and find that every time i use it, i...
    3. Modifying a file in place perl -i
      Hi, I am trying to modify some file sin place usinh the -i switch of perl. In each of the files however I need to replace certain strings with...
    4. Saving Perl output
      What I want is when some one clicks a link a box appears asking where to save to, same as when you download a zip etc. Then for the comtent of the...
    5. Modifying output in Repeater
      I am having trouble figuring out how to modify the databound field data that is bound to a Repeater control, before it is output to the browser. I'm...
  3. #2

    Default Re: Modifying a HP/GL2 output file using Perl

    Steve Hemond wrote:
    > Hi gurus,
    >
    > I read some docs about Perl and I think it would be the best language to
    > suit my acutal need.
    >
    > I have to change plotter pen widths from an HP/GL2 output file with specific
    > widths. An HP/GL2 is a plain-text file filled with plotter commands. The
    > one I need to check are :
    > PSx (where x = pen number) = PEN SELECT
    > PWx (where x = width) = PEN WIDTH
    >
    > In that kind of file, you will see PS1 followed by a bunch of coordinates
    > and stuff. From there you can see that every command after the PS1 is for
    > the PEN NUMBER ONE. I have to check where the PW is, and affect a new width
    > right after it. I have to do this to all pens. I already know which width
    > goes for each pen. My problem is that the number of caracters between each
    > PSx and PWx are different from time to time.
    >
    > Unless anyone has a better idea it would probably need to read the file byte
    > per byte, and when a PSx is read, the program will remember the current
    > pen, and when reaching the next PWx, it will affect the corresponding width
    > for the current pen.
    >
    > Is there an quick and effective way to do such kind of things with Perl?
    > From what I've read, I can make a Perl script read a file LINE by LINE but
    > I don't know about reading byte per byte.
    >
    > Any ideas?

    Unless your files are huge (>10 Mb), you should probably slurp them into
    a string, operate on the string with a well-crafted substitution, and
    write it out again. Perhaps something like:

    use strict;
    use warnings;
    my $in;
    my %width=(1=>3,2=>8,3=>5); # pen number => width
    {local $/;$in=<DATA>;}
    $in=~s/PS(\d+)(.*?)PW(\d+)/"PS$1$2PW".$width{$1}/egs;
    print $in;
    __END__
    xxxPS1blahblahPW13blahblahPS2blahPW1blahPS3blahPW2 etcetc
    and some more PS1 blah PW123 blah blah blah

    That outputs:

    D:\junk>perl junk363.pl
    xxxPS1blahblahPW3blahblahPS2blahPW8blahPS3blahPW5e tcetc
    and some more PS1 blah PW3 blah blah blah

    D:\junk

    The above requires that there be no whitespace between the PS and the
    digit follow, and the same for the PW field. It also assumes there will
    be only one PW field following a given PS field (if there could be more
    than one, something slightly fancier would be needed). Note that if you
    neglect to assign a given pen a width value, the corresponding PW field
    will not have a digit.


    ....

    > Steve
    --
    Bob Walton

    Bob Walton Guest

  4. #3

    Default Re: Modifying a HP/GL2 output file using Perl

    Steve Hemond <shemond@hotmail.com> wrote in comp.lang.perl.misc:
    > Hi gurus,
    >
    > I read some docs about Perl and I think it would be the best language to
    > suit my acutal need.
    >
    > I have to change plotter pen widths from an HP/GL2 output file with specific
    > widths. An HP/GL2 is a plain-text file filled with plotter commands. The
    > one I need to check are :
    > PSx (where x = pen number) = PEN SELECT
    > PWx (where x = width) = PEN WIDTH
    >
    > In that kind of file, you will see PS1 followed by a bunch of coordinates
    > and stuff. From there you can see that every command after the PS1 is for
    > the PEN NUMBER ONE. I have to check where the PW is, and affect a new width
    > right after it. I have to do this to all pens. I already know which width
    > goes for each pen. My problem is that the number of caracters between each
    > PSx and PWx are different from time to time.
    >
    > Unless anyone has a better idea it would probably need to read the file byte
    > per byte, and when a PSx is read, the program will remember the current
    > pen, and when reaching the next PWx, it will affect the corresponding width
    > for the current pen.
    >
    > Is there an quick and effective way to do such kind of things with Perl?
    > From what I've read, I can make a Perl script read a file LINE by LINE but
    > I don't know about reading byte per byte.
    Bob Walton has suggested slurping the file, and that looks indeed
    attractive because the content of one line can influence what happens
    many lines later.

    However, the problem can be dealt with line-wise too.

    If you work through the file and pick up a new line, split the
    line into parts where a "PSx" is found. Then in each part all
    "PWx" must be changed in a uniform way. The width for the first
    part will be the one that has been used for the last part of the
    last line. The width of the other parts is determined by the "PSx"
    statement they begin with. You will have to keep the last width
    used for the next line you pick up. So a function to deal with a
    line would take a line to change and the initial width for arguments,
    and return the changed line and the new width:

    sub deal_with_line {
    my ( $line, $width) = @_;
    my @parts = split /(?=PS\d+)/, $line; # note zero width pattern
    for ( @parts ) {
    $width = $width{ $1} if /^PS(\d+)/;
    next unless defined $width; # may happen before the first "PSx"
    s/PW\d+/PW$width/g;
    }
    ( join( '', @parts), $width);
    }

    The main program becomes:

    my $width;
    while ( <DATA> ) {
    ( $_, $width) = deal_with_line( $_, $width);
    print;
    }

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139