Unless your machine has an unusually small amount of memory (<1GB) it is quite hard to see why you would be running out of memory on a 400MB file.
There are some anomolies in your code (you calculate $buf_size but never use it; you use & on sub calls which you shouldn't), but nothing that stands out as being the cause of the problem.
You are passing a string containing the whole file into to_ascii() and then copying it; and then passing the modified copy back and then copying it again; which can be alleviated by using references as below. Try that and see how you get on:
#!/bin/perl # File: get-count.pl # # Perl script to locate the trailer record of a Fiserv data file and r +eturn # the following information # # Usage: # perl get-count.pl INPUT_FILE # # Number of records per the trailer # Number of records based on the file size and record size # Record size. sub to_ascii { my( $s ) = @_; $$s =~ tr[\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d\x4d\x5d\x5c\x4e\x6b\x60 +\x4b\x61\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\x7a\x5e\x4c\x7e\x6e\ +x6f\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6\x +d7\xd8\xd9\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xad\xe0\xbd\x5f\x6d\x79\x8 +1\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96\x97\x98\x99 +\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xc0\x6a\xd0\xa1] [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\ +x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x +3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x5 +0\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61 +\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\ +x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e]; return; } sub file_info { my( $file ) = @_; open( FP2, "<$file" ) || die return( -1, -1, -1 ); binmode( FP2 ); my( $file_size ) = -s $file; my($s) = ''; seek( FP2, -$file_size, 2 ); read( FP2, $s, $file_size ); to_ascii( \$s ); if( $s =~ /999 (\d\d\d\d\d\d\d\d\d)( +$)/ ) { my( $rec_size ) = 4 + length( $1 ) + length( $2 ); my( $rec_count ) = $1 + 0; my( $rec_count_calc ) = ( $file_size / ($rec_size) ) - 1; return( $rec_count, $rec_size, $rec_count_calc ); } else { return(-1, -1, -1); } close(FP); } foreach $file ( @ARGV ) { my( $rec_count, $rec_size, $rec_count_calc ) = file_info( $file ); print "File: $file\nRecord Count: $rec_count\nComputed Record Coun +t: $rec_count_calc\nRecord Size:$rec_size\n"; }
In reply to Re: Perl Out of memory error
by BrowserUk
in thread Perl Out of memory error
by vdel2393
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |