Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Repeating Code - there has GOT to be a better way!

by cleverett (Friar)
on Mar 31, 2010 at 17:43 UTC ( #832102=note: print w/replies, xml ) Need Help??

in reply to Repeating Code - there has GOT to be a better way!

You need to make it so you can run the same code for every one of the files. The thing that changes every time is %<name>Cmd.

One question to ask is: must all the %<name>Cmd hashes in the .pm files have different names?

If yes, you need alias %<name>Cmd onto %Cmd or something like that. This is kind of cool, but not recommended to do routinely.

If no, then just rename the %<name>Cmd hashes in each of the .pm files to %Cmd.

Either way, you can process all the files in a loop or a subroutine (or both) with code that looks something like this:

our %Cmd; do "db/$name" . ''; $ApidOffset = $Application{$name}{AppApidOffset}; foreach $cmd (keys %Cmd) { if (int($fssbCmd{$cmd}{fixed_pattern}) <= 15) { $opcode = sprintf("0%x",$Cmd{$cmd}{fixed_pattern}); } else { $opcode = sprintf("%x",$Cmd{$cmd}{fixed_pattern}); } $apid = sprintf("%x",$ApidBase+$ApidOffset); $cmdData{$cmd} = {opcode => "0x$opcode", apid => "0x$apid"}; };
Hope this helps.

Replies are listed 'Best First'.
Re^2: Repeating Code - there has GOT to be a better way!
by jedikaiti (Friar) on Mar 31, 2010 at 17:49 UTC

    I have no control over what is in the .pm files, so I can't make them all the same name.


    Swiss Army Nerd

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://832102]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2016-10-22 22:52 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (299 votes). Check out past polls.