in reply to Re^2: Question about warnings and arrays
in thread Question about warnings and arrays

Hi james28909 , yes, I'm that guy, you know what I'm going to say :) don't nest loops, write subroutines, small subroutines, easy to debug ... naturally this code is untested but looks easier to read doesn't it :) instead of bunches of comments, subroutine names
#!/usr/bin/perl -- ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use autodie qw/ open close /; use Path::Tiny qw/ path /; Main( @ARGV ); exit( 0 ); sub Main { my( $dirname ) = @_; my @files = getFiles( $dirname ); my @pointers; for my $file ( @files ) { SolveThisProblem( $file, \@pointers ); } SpewPointers( 'temp', \@pointers ); } ## end sub Main sub SpewPointers { my( $outfile, $pointers ) = @_; my $tempfh = path( 'temp' )->openw; ## just like autodie dies o +n error my $ix = 0; for my $lines ( @$pointers ) { ++$ix; print $tempfh "Pointer$_ - $lines"; } close $tempfh; } ## end sub SpewPointers sub SeekToAbcOffset { my( $infh ) = @_; my $infhsize = -s $infh; #seek to text entry reference in file seek $infh, 6, 0; read $infh, 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 file/proces +s info seek $infh, $offset, 0; print "\n\n$infh - size of file: $infhsize - Text is at offset: $a +bc\n\n"; return $offset; } ## end sub SeekToAbcOffset sub SolveThisProblem { my( $filename, $pointers ) = @_; use autodie qw/ open close /; open my( $infh ), '<', $filename; binmode $infh; my $offset = SeekToAbcOffset( $infh ); READER: while( read( $FILE, my $by, 1 ) ) { if( $by eq "\x00" ) { my $pos = tell( $FILE ); my $decimal_value_pointer = $pos - $offset; push @$pointers, sprintf( "%X", $decimal_value_pointer ); next READER; } elsif( $byte =~ /ff/ ) { last READER; } } ## end READER: while( read( $FILE, my $by...)) } ## end sub SolveThisProblem __END__

See also perlquote and perlrebackslash because "\x00" is equal to chr(0), ie  $by eq chr(0)

Replies are listed 'Best First'.
Re^4: Question about warnings and arrays
by james28909 (Deacon) on Aug 13, 2014 at 11:04 UTC
    you guys are awesome as always :)
    i will go back and revise my code with hopes it comes out this neat. BUT i need also to know how i can reopen each file and write these pointer values back. See, what this programs main goal is, is to fix another programs mess ups lol. This program i have written goes to text entry and gets the pointer value, which should be written in the upper half of the file and overwriting the incorrect pointer value. if i were to push all the pointers to the array, would it work right if i did a foreach my $line(@array) as to where i could open the files and re write the values in succesion?

    OR better that than, i could push pointer to an array, and before closing the file, i could go ahead and do the foreach $line(@array) which would hopefully write the pointers as their correct offset. then once all pointers are written, i could clear the array and open next files and start again.

    I guess my next question is, can i loop thru an array like that? and expect the outcome to be right? and apologies for taking so long to respong

      Well, theoretically the answer to three of your questions is yes, it's possible, if you code it correctly ;-)

      Other than that, we'd need to see some example expected output and some of the code your questions are about. Please have a look at I know what I mean. Why don't you? (as well as the usual How do I post a question effectively?).

      In general, you can open a binary file for editing via open my $fh, '+<:raw', $filename or die $!;

        let me give an example:
        use File::Slurp; open ( my $file, '<', "file" ); binmode($file); my @array = read_file ("pointers"); seek ( $file, 0x67, 0 ); print $file @array[0]; foreach ( my $line(@array){ chomp ( $line ); seek ( $file, 20, 0 ); print ( $file $this ); }
        this is the code i have tried. it will write to the file, but not in an expected way. As you know i do most of my work/programs on binary files. so the data being pushed to the array will have to be written back to the file, but as byte characters/hexadecimal characters and in raw format.

        so when it puts "29" into the array, i need it to write 29 in the raw file as a byte, and the code above writes it as a character 29 (which is 32 39 in plain text).

        The open file is in raw or binary mode, but it writes in text mode from the array. i need to write in binary mode. if you run the script on the files i posted earlier, then you can make a file with this in it:
        21 4B A8 C6
        and read that into an array, then try to write from the array to a raw file, it doesnt work, and looks to still write to the raw file in text mode. and if i try to binmode the array, it throws errors as expected.

        im am very confused at this point, as i need to write the pointers back to the file but in raw/binary mode. and i am unsure how to do that from an array because it looks as if it writes back to a raw file in text mode.