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 Papers

my 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.