Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

by CountZero (Bishop)
on Mar 31, 2010 at 19:02 UTC ( #832122=note: print w/replies, xml ) Need Help??

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

I see two possible solutions:
  1. Although you cannot change the files, nobody says you have to use them "as is": you could pre-process them and turn them into a more workable data-format and then work with this data; or
  2. You could write a subroutine that takes two arguments: the first is the <name> and the second is the %<name>Cmd hash:
    sub process { my ($name, %Cmd) = @_; $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"}; }; }
    Of course you will still have 11 subroutine calls to make, but I think you can survive that!

Update: fixed a typo in the subroutine (%Cmd instead of %cmd)


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://832122]
[Corion]: Oooh - I had another devious idea - "Host C" - a C language where every struct is 4K in size. This makes memory and disk access incredibly fast ;)
[Corion]: (because everything is aligned to a memory page and all pages can be read+written directly from disk without buffering)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-04-25 08:55 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.