I made some slight changes your program (as currently posted in the present thread), and it ran on a slightly modified version of the data provided in the earlier thread, with no "out of memory" problem.
I don't think my code changes had anything to do with memory management -- I just fixed the syntax errors, and changed to using STDERR for the status messages. In order to get it to work on the previously posted "postfix.txt" file (about 64KB), I had to delete the "." characters at the end of the data, in order to get past the "Bad $op ..." die condition.
#!/usr/bin/perl -w use strict; use warnings; $|=1; sub multiply { my( $ref1, $ref2 ) = @_; my @temp; for my $r1 ( @{ $ref1 } ) { for my $r2 ( @{ $ref2 } ) { push @temp, $r2 . $r1; } } return \@temp; } $/ = ','; my @stack; open POSTFIX, '<', 'postfix.txt' or die $!; while( my $op = <POSTFIX> ) { chomp $op; $op =~ tr/\r\n//d; ## remove any trailing newline if( $op eq 'x' ) { print STDERR "MULTIPLY\n"; push @stack, multiply( pop( @stack ), pop( @stack ) ); } elsif( $op eq '+' ) { print STDERR "ADD\n"; push @stack, [ @{ pop @stack }, @{ pop @stack } ]; } elsif( $op =~ m[^\d+$] ) { push @stack, [ pack 'v', $op ]; } else { die "Bad '$op' at position;" . tell( POSTFIX ); } } print STDERR "stacksize: " . @stack. "\n"; for my $item ( @stack ) { printf "%s", join ',', unpack 'v*', @{ $item }[ 0 ]; for my $group ( @{ $item }[ 1 .. $#$item ] ) { printf "+%s", join ',', unpack 'v*', $group; } print "\n"; }
With my version, you can redirect STDOUT to a file, and just see the status messages on your terminal. With a bash-like shell, you can also redirect STDERR to a separate file, which is what I did.
When the run finished successfully, the STDERR log file had 5170 lines containing "MULTIPLY" and 3966 lines containing "ADD", which happens to match the number of "x" and "+" characters in the data file, respectively. There were 30 elements in the "@stack" array, and 30 lines of data in the STDOUT file, the first two lines being:
The third line was over 8 KB long, the fourth line was:1 2,5,6+2,4,6+2,3,6
The fifth line was about 150 KB long, the 7th was 658 chars, the 28th was 228 chars, and all the rest had a single 4-digit number. The number on the 30th line was "9166", which is happens to be the number of digit strings in the postfix.txt input file.40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55
I have no idea what this sort of output is supposed to mean. Perhaps I've missed something, but I think your statements about the intent of your program, and the nature of your approach, consist so far of the following:
I wrote a program to sybolically process multiplications on an equation in postfix notation. (taken from the first post)
The goal is to convert "if-then-else" statements in a 4th GL scripting language into equations. For example "if(a) {}elsif (b) {} else {}" would becomes (a+b+c).
Once accomplished all paths through the 4th GL script can be found by "multiplying" the equations through. For example, two simple if/else statements could be represented as (a+b)(c+d.) The independent paths throught the code is: ac+ad+bc+bd.
The code provided here does this equation transformation. The difficulty is that the equation is 64K in characters and will result in a couple million independent elements as a result.
Sorry -- not only do I fail to get a clear idea of the goal, but I really can't see any relation between that description and the output that I got from your script. For that matter, I'm clueless about what the output data provides, let alone what the input represents. Still, I hope the information I provided was helpful in some way.
In reply to Re: Out of Memory 2.
by graff
in thread Out of Memory 2.
by dneedles
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |