in reply to Z80 Assembler -> Hex Opcode

You do realise that you are forcing perl to redefine your hash with every call to your sub don't you!!!! By moving the hash definition out of the sub this will run so many orders of magnitude faster it is not funny (roughly 50,000 lines per second in my testing on a mid range single processor PIII 1.2GHz server....)

# first define our hash (just the once not for every bloody line!!!!!! + :o) my %opcodes = ( blah ); open (SOURCE, "<z80code.s") or die "Couldn't open source file."; open (DEST, ">z80code.o") or die "Couldn't open destination file."; while (<SOURCE>) { print DEST encode($_)."\n"; } sub encode{ # blah }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Z80 Assembler -> Hex Opcode
by steves (Curate) on Feb 09, 2003 at 11:36 UTC

    Maybe he's trying to emulate the speed too. 8-)

      Heh heh heh

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: Z80 Assembler -> Hex Opcode
by Elgon (Curate) on Feb 09, 2003 at 12:52 UTC

    Hi Tachyon,

    I did indeed realise this, however in the code as you have written it the declaration of %opcodes is in a different scope to that of the subroutine and hence, all you get returned is an error. I am not sure exactly how to access the hash from the subroutine's scope - %main::opcodes ???

    I did also originally run the program without the encode routines as a separate sub and it didn't appear to run any faster.

    Thanks,

    Elgon

    Update: I tried running the program as a simple loop, without the sub as before, and it took about 1 second instead of 36 or so to run. Must have declared the hash inside the loop the first time. Oops. Time to go back to the Blue Camel and re-read the sections on scoping and packages :-)

    Update^2: I've been trying to do what you suggest by passing a reference to %opcodes to the subroutine along with the line and then saying local *hash = shift; a la blue camel page 294 but this isn't liked either. FYI I'm using Perl 5.005 so use of our is right out.

    Update^3: See root node.

    "What this book tells me is that goose-stepping morons, such as yourself, should read books instead of burning them."
           - Dr. Jones Snr, Indiana Jones and the Last Crusade

      With the example I posted %opcodes is in main::, so too the sub is in main:: - ergo and therefore same scope, no errors, runs as posted. You don't need the use vars *and* the my declaration for %opcodes. One or the other will pacify strict...

      # declare this in implicit main:: package my %here = ( in => ' main' ); run(); sub run { # we are still in main:: here, so we can access %hash just fine print "I am still ", %here }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Ah, found the problem,

        a) poor understanding of scoping on my part and b) when I first ran as you described, I had the declaration of %opcodes after the main loop containing the call to encode() so, in effect, the declaration never took place until after the program had effectively run its course.

        You live and learn.

        Elgon

        "What this book tells me is that goose-stepping morons, such as yourself, should read books instead of burning them."
               - Dr. Jones Snr, Indiana Jones and the Last Crusade