Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-01-19 01:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.