Hello monks, i have come before you today to ask a question in which i am unsure about. a couple of them actually. If i need to make a seperate thread for the questions i will, but i think the first question can be summed up pretty quick. It is about use warnings;
For example, the code i have created will run fine and dandy with strict and warnings on, and i will even get the expected results perfectly. But when warnings are used, i get an error about a $FILE being closed. and like i said, even tho i get this warning, it still completes the script and i get expected results. i will provide example files and the script later in this post that you can compare with :) . thats it for the first question
The next question is about populating an array. The main problem at hand is, i will have different amounts of files i will be doing this batch script on. It seems the way that i have coded this, i will have a hard time storing my needed precious data into a variable. sSo i think it will be best to use an array with delimiters, and foreach file, write in a delimiter and then split that array into multiple other arrays (or multidimensional) i am not sure which would be the best way honestly. Let me post the code that covers these two questions, with hopes you understand it ( i tried to comment in everywhere to help you understand better the code):
use strict;
# use warnings;
my $file;
my $dirname = $ARGV[0]; #warn "$dirname";
my @array;
foreach my $file (<$dirname/*>) {
next if -d $file;
open( our $FILE, '<', $file );
binmode($FILE);
#get filesize of file in process
my $filesize = -s $file;
#seek to text entry reference in file
seek $FILE, 6, 0;
read $FILE, my $buf, 2;
#convert data
my $abc = unpack('H*', $buf);
my $offset = hex($abc);
#use text entry reference to seek to actual text and print fil
+e/process info
seek $FILE, $offset, 0;
print "\n\n$file - size of file: $filesize - Text is at offset
+: $abc\n\n";
#loop thru file byte by byte doing processes depending on
+regex matches
while (read($FILE, my $by, 1)){
#convert contents of byte to hex string
my $byte = unpack('H*', $by);
#if regex match is 00 return position in file of m
+atch, and
#subtract offset to get total bytes read from midd
+le of the file
if ($byte =~ /00/){
my $pos = tell($FILE);
#this subtraction will later be converted to a hex
+ string "0xXX"
my $decimal_value_pointer = $pos - $offset;
my $pointer = sprintf("%X", $decimal_value_pointer
+);
push (@array, qq($pointer\n));
#just prints correct pointer value in hex
#print "pointer is: 0x$pointer\n\n";
next;
# close $FILE;
}
#if $byte is ff then close file,which will jump ba
+ck to the next file in the foreach loop
if ($byte =~ /ff/){
close $FILE;
}
}
}
unlink ('temp');
open my $temp, '>>', "temp";
foreach my $lines(@array){
++$_;
print $temp "Pointer$_ - $lines";
}
in this code above, i am reading a directory. and for each file in this directory i am opening it, getting filesize, then seeking and reading particular data i need out of the file. This is VERY HARD to explain sorry. but after i open the file and get filesize and an address reference from 0x07 - 0x08, i seek to the value stored in $offset. once i seek to $offset, i read the file byte by byte with the while loop.
I am seeking for "00" and "ff" in hex string, and when i find them i either return position in the file that i am at, then subtract from that $offset which gives me a decimal number that i convert to hex. i need this nuber to stay in hex as it is very very important (it is a pointer to each individual sentence in the text entry offset formerly known ass $offset. once i convert $decial value pointer to its hex equvalent, i push it to an array with a new line at the end. then later on in this file, after i have parsed thru all the 00's, it will find "ff" which is in every file (so i dont need to read to eof) and it will close $FILE and move to the next one perfectly. will do this over and over till there are no more files :)
Now at the end, what i did was my failed attempt to label each one of these, and that is where i beg of your help. there will always be an unknown amount of files, therefore, there will be an unknown amount of $pointers. These $pointers will have to be written back to the file at static addresses. these addresses are known. But i would like a way i could either dynamically create arrays for each file, or what would be even better is to figure out how i can do some thing like this....
pseudo code of course lol:
foreach my $line(@array){
foreach my file(0 .. 100 or however would be best){
open $file
seek to static offset
write $line from array to $file
}
}
this would be optimum if i could keep the @array as a whole, and just write each value where i need it, even while opening and closing files, and hopefully you understand what i mean. here are the Files i promised to test with:
Test Files
and just extract the script and folder anywhere (it is in another folder already) and look at the code, and please, if i cn be any more clear on anything or if you see something wrong with my code please dont hesitate to tell me :)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.