unpack is for parsing binary files. For example the 'x' in your $format doesn't parse a space but a real byte 0x00 nowhere to be found in your example data. I don't say it is not possible to use unpack to parse text files, but it is like using a soldering iron to repair your wrist watch.

You seem to have some other misconceptions about unpack. Your line 40 makes no sense. The assignment of ($1,$3,$5,$7... to the variables will not influence the assignement from unpack later to only pick the first, third and so on of the parsed items

Use regexes instead. For example your title line can be parsed by

($CardNo, $MMDD, $YY, $StormNo, $TotalNo, $Name, $XING, $SSS)= m{^(\d+)\s+ (\d\d/\d\d) /(\d+)\s+ #the date M=(\d+)\s+ (\d+)\s+ SNBR=\s*(\d+)\s+ (.+) \s* # arbitrary name or NO NAME XING=(\d+)\s+ SSS=(d+) \s* $}x;
(I assumed here that instead of NO NAME any arbitrary string could be in that place)

Note that I use \s+ to parse a space. That makes the parsing more robust, because it doesn't matter if there is a tab character instead of a space or more than one space. Also I use \s* in places where spaces are optional

m{} is the same as //, it is just nicer if you have a '/' to parse, you don't need to escape it. The x lets me use spaces and comments in the regex

I kept the regex simple, you should be able to construct the regex for the other lines from this. Just read some more about regexes in a good perl book or online documentation.


In reply to Re^3: Parser help by jethro
in thread Parser help by MKevin

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.