in reply to Out of Memory 2.
Two of the print statements in your sample code appeared to be out of place, with one causing a compile failure. I modified it to the following:
#!/usr/bin/perl 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; chop $op if $op =~ tr[\n][\n]; ## remove any trailing newline if( $op eq 'x' ) { print "MULTIPLY\n"; push @stack, multiply( pop( @stack ), pop( @stack ) ); } elsif( $op eq '+' ) { print "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 "stacksize: " . @stack; 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"; }
Then I put the sample expression you provided "(a+b)(c+d.)" in a file named postfix.txt as follows:
and ran the script.(a+b)(c+d.)
It produced the following output:
Bad '(a+b)(c+d.)' at position;12 at ./test.pl line 38, <POSTFIX> chunk + 1.
This isn't surprising as there is nothing in your code to handle the parentheses. Nor is there anything to handle the alphabetic characters, if it got past the initial parenthesis.
It also appears that your sample expression is infix but your code is written to work with postfix expressions.
As GrandFather said, it would be very helpful if you provided a small working sample of input to your program.
It appears that you are trying to produce a large number of combinations. With input of approximately 64K corresponding to nothing other than a series of simple if/else blocks, with something on the order of 4 characters per block, then you would have approximately 16K blocks. If I understand what you are doing correctly, you would then produce all combinations of either branch of each if/else statement. This corresponds to approximately 2**16384, a very large number indeed. You may find that producing and storing all possible combinations exceeds the capacity of your database.
In summary, you need to explore and understand the magnitude of the data set your are trying to produce before deciding how to store it.
Depending on what you want to do with the data once it is produced, you may find it is better to use and discard it as you produce it, rather than producing and storing it in one pass then reading and using it in another.
Update: I missed the link to Out of memory. and the sample data there. Now I see that the example expression in your post is not an example of intended input to your program but how about posting a small but complete sample input?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Out of Memory 2.
by BrowserUk (Patriarch) on Oct 26, 2008 at 00:01 UTC | |
|
Re^2: Out of Memory 2.
by dneedles (Sexton) on Oct 25, 2008 at 23:25 UTC |