Hey, I'm trying to Perl some BIO past papers (a British programming contest) but I have some results that I can't explain. The program has to take in a string where b, i and o are functions and have coefficients and groupings such as b3(io) does b then io 3 times. Full explanation is here (question two):
BIO Past Papersmy script so far is as follows, you can probaby work out what I'm trying to do here:
#!/usr/bin/perl # Shuffles cards @pack = (1, 2, 3, 4, 5, 6, 7, 8); sub b { push(@pack, shift @pack); } sub i { my ($a, $b, $c, $d, $e, $f, $g, $h) = @pack; @pack = ($e, $a, $f, $b, $g, $c, $h, $d); } sub o { my ($a, $b, $c, $d, $e, $f, $g, $h) = @pack; @pack = ($a, $e, $b, $f, $c, $g, $d, $h); } sub parse { my ($string) = @_; print "\nParse $string: "; foreach $offset (0..(length($string)-1)) { $char = substr($string, $offset, 1); if ($char =~ /b|i|o/) { print "bio "; &{$char}; } elsif ($char =~ /\d/) { print "digit "; # foreach (1..$char) { &parse(substr($string, $offset+1)); +} } elsif ($char =~ /\(/) { print "open "; &parse(substr($string, $offset+1, (rindex($string, ")", $o +ffset)-$offset)+1) ); } elsif ($char eq ")") { print "close "; } } } chomp($shuffle = <STDIN>); &parse($shuffle); print "\n@pack\n";#
I don't know why this prints:
oib(oib) Parse oib(oib): bio bio bio open Parse o: bio bio bio bio close 1 4 5 8 2 3 6 7
when it should print
oib(oib) Parse oib(oib): bio bio bio open Parse oib: bio bio bio # whatever here
Firstly the string doesn't seem to get passed correctly, and then it parses it wrong anyway (too many bios). Anyone wanna try this one?
Thanks, Dom.
In reply to Shuffling cards by amnesty_puppy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |