Hi All

I was wondering if someone can enlighten me on the following behaviour with regard to a particular code snippet

Code is:

<snip>

$line = pack ("A1170") ; substr ($line, 0, 135) = " " ; substr ($line, 136, 11) = "DATE=${mdy}" ; substr ($line, 147, 6) = "${mdy}" ; substr ($line, 153, 6) = "${hms}" ; substr ($line, 159, 3) = " " ; substr ($line, 162, 17) = " " ; substr ($line, 179, 8) = "G1ADP" ; substr ($line, 187, 3) = "$CLIENT_ID" ; substr ($line, 190, 507) = " " ; substr ($line, 508, 1) = " " ;

<snip>

Basically I deal with outputting various data in fixed width format and like the above method as it references back quite nicely to file definitions that I follow to create the output. For instance, the above is the definition for a file header

But, I've been ignoring a couple of interesting "funnies" with this way of outputting fixed widths. These being:
1) My $line should be 510 spaces long. But if I try to set A510 I get the old substr outside of string at error and for some reason I have to set it to the value of 1170 (found by trial and error).
2) If the last field is space, then I have to add in an extra output to get the file length outputted correctly. As in the above substr ($line, 508, 1) = " " ;.
Can anyone enlighten me on what's occurring? I expect it's user error :-)

Cheers, Jake (aka spudulike)


In reply to Google has failed me! Using pack and substr for fixed width file output by spudulike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.