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

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

by CountZero (Chancellor)
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)

CountZero

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


Comment on Re: Repeating Code - there has GOT to be a better way!
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-04-18 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls