To begin with, your code:

  1. open( our $FILE, '<', $file ); is better with error handling and a lexically scoped filehandle: open my $fh, '<', $file or die $!;
  2. read is also better with error handling: read($FILE, my $buf, 2)==2 or die "failed to read 2 bytes: $!";
  3. You should probably look at the pack documentation: your first unpack could be just my $offset = unpack('n',$buf);, and your second unpack could be my $byte = unpack('C',$by);, and then instead of regexes you could just write if ($byte==0) ... elsif ($byte==0xff)
  4. Several of your variables could have better names, it'd make thinking and talking about the code easier: @array, $abc, $temp, $lines
  5. Why unlink ('temp'); open my $temp, '>>', "temp"; when you can just open my $temp, '>', "temp" or die $!;?

Now your questions:

You should be using warnings, don't comment it out! The warning you're getting is correct - you're trying to read a closed filehandle. You're essentially forcing the loop to end via an error by simply closing the file. Instead you should be using last to exit that loop (that's an alternative to SuicideJunkie's suggested approach above).

You mention "an array with delimiters" - that doesn't sound like a good idea, it sounds like what you're looking for is an array of arrays. I suggest you study that page well, along with perlreftut; references are further documented in perlref. Basically, for each file you could declare and populate an array my @pointers (declared inside the foreach), similar to what you're doing with @array now, and then at the end of the foreach you would be adding a reference to that array to @array, with push @array, \@pointers;. If you use that approach, Data::Dumper is very useful visualizing the data structure you've created for understanding what's going on and for debugging: use Data::Dumper; print Dumper(\@array);.

Unfortunately your description of "These $pointers will have to be written back to the file at static addresses" is too unclear for me to make good recommendations. You want to take every "pointer"/"line" from your source files and write these to hundreds of other files? Or back to the source files? Do these files already contain data that you are overwriting, or are you generating these files yourself? I think what would really help is some examples of your desired output.


In reply to Re: Question about warnings and arrays by Anonymous Monk
in thread Question about warnings and arrays by james28909

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.