You've got lots of problems here. First off, while (<file>) doesn't do what you think it does. While will actually return the contents of a file line by line, where "line" is defined by "some data that ends in a \n" (actually, it's "some data that ends with the contents of $/" where $/ defaults to \n). Anyway, as written your code will pull in one newline separated line at a time, not an entire "record" like it appears you want.

In this case you don't want to read the record line-by-line. You want to read it record-by-record, where each record is delineated by a "Begin Product/End Product" pair. This is what I'd do in a case like this: (untested code ahead)

open (FILE, $lfilename) or &dienice; $/ = undef; # Slurp mode $file = (<FILE>); # Grab the whole file into $file while ($file =~ m/Begin Product(.*?)End Product/gs) { my $record = $1; # Now $record contains the entire contents of exactly one # record from your file. You can now fold, spindle, and # otherwise mutilate $record to pull out the various # and sundry pieces for each record. }
As far as the folding, spindling, and mutilating goes you're on your own. Your file format is so irregular that you'll have to meticulously parse it bit by bit. Something like "split" only works on regular formatted records, which you don't have here. You may end up doing additional regex matches for "Begin Option/End Option" within each record. It ain't pretty; the more irregular your data is the uglier the code is going to be extracting it.

Gary Blackburn
Trained Killer

Update: Corrected a stupid error. This is why I shouldn't code this late at night. :-P


In reply to Re: how to hash this by Trimbach
in thread how to hash this by malaga

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.