Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Question about warnings and arrays

by SuicideJunkie (Vicar)
on Aug 12, 2014 at 19:43 UTC ( [id://1097173]=note: print w/replies, xml ) Need Help??


in reply to Question about warnings and arrays

Just as a note, using both $file and $FILE for variable names is going to cause pain. Why not name them $filename and $iFH/$oFH or something else unique?

Also, a nicer way to close a filehandle is to allow the variable to go out of scope. Just my $filehandle it inside the outer loop, and use next FILE; with an appropriate "FILE" label on the outer loop instead of closing it manually. The filehandle will get closed for you when you leave, and $filehandle won't exist after the file is closed so you can't accidentally try to do operations on the closed filehandle.

That's a bit nicer than closing the file in order to make your read throw an error in the while loop, thus breaking out to the outer loop.

Replies are listed 'Best First'.
Re^2: Question about warnings and arrays
by james28909 (Deacon) on Aug 12, 2014 at 20:09 UTC
    so when i come out of a loop like that, it will close the $FILE for me? but i think in this particular case, dont i need to keep it open until $bytes =~ /ff/ ? that way it closed the file and goes to the next one early. but i thnk i see what your saying :) i will def keep that in mind. i just need to figure out how to make a list of variables that i can store $pointer into. or read each line from the file thats created into $_ and write that at my offsets i need to then close file and open next without loosing spot in pointers file. edit, i kind of see what you mean, but a good example would be even better :P
    you know you want to give an example anyway :P
      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)

        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
      so when i come out of a loop like that, it will close the $FILE for me?

      Yes, but only if you use my $FILE instead of our $FILE. The automatic close is explained in open (look for the term "scope").

      so when i come out of a loop like that, it will close the $FILE for me

      Only insofar your filehandle is lexical (using my, not our, as already suggested), and is lexically scoped to that loop's block. So you'd better understand Perl scopes if you want to use this opportunity.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found