Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Question about warnings and arrays

by Anonymous Monk
on Aug 12, 2014 at 23:37 UTC ( [id://1097197]=note: print w/replies, xml ) Need Help??


in reply to Question about warnings and arrays

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.

Replies are listed 'Best First'.
Re^2: Question about warnings and arrays
by james28909 (Deacon) on Aug 13, 2014 at 13:27 UTC
    thanks. you havent steered me wrong yet :)
    i guess it is time to study more lol

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1097197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-24 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found