dneedles has asked for the wisdom of the Perl Monks concerning the following question:
However, it ran out of memory as well. Any insights in regards to approach is appreciated.$|=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 print "MULTIPLY\n"; if( $op eq 'x' ) { push @stack, multiply( pop( @stack ), pop( @stack ) ); } print "ADD\n"; elsif( $op eq '+' ) { 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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Out of Memory 2.
by graff (Chancellor) on Oct 25, 2008 at 18:43 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 20:13 UTC | |
by apl (Monsignor) on Oct 25, 2008 at 20:53 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 21:00 UTC | |
|
Re: Out of Memory 2.
by Illuminatus (Curate) on Oct 25, 2008 at 17:19 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 19:56 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 20:27 UTC | |
|
Re: Out of Memory 2.
by ig (Vicar) on Oct 25, 2008 at 22:40 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2008 at 00:01 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 23:25 UTC | |
|
Re: Out of Memory 2.
by BrowserUk (Patriarch) on Oct 25, 2008 at 23:40 UTC | |
by dneedles (Sexton) on Oct 26, 2008 at 00:05 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2008 at 00:27 UTC | |
|
Re: Out of Memory 2.
by GrandFather (Saint) on Oct 25, 2008 at 21:27 UTC | |
by dneedles (Sexton) on Oct 25, 2008 at 21:42 UTC | |
by GrandFather (Saint) on Oct 25, 2008 at 22:02 UTC | |
|
Re: Out of Memory 2.
by JavaFan (Canon) on Oct 25, 2008 at 23:37 UTC | |
|
Re: Out of Memory 2.
by graff (Chancellor) on Oct 27, 2008 at 01:12 UTC | |
by Anonymous Monk on Oct 27, 2008 at 01:45 UTC |