<?xml version="1.0" encoding="windows-1252"?>
<node id="832090" title="Repeating Code - there has GOT to be a better way!" created="2010-03-31 12:58:03" updated="2010-03-31 12:58:03">
<type id="115">
perlquestion</type>
<author id="826783">
jedikaiti</author>
<data>
<field name="doctext">
&lt;p&gt;Today's mission, if you choose to accept it, is to help me deal with some very repetitive code. I have a script that works beautifully, but has a problem. It repeats this code about 11 times:&lt;/p&gt;
&lt;code&gt;
our %&lt;name&gt;Cmd;
do 'db/&lt;name&gt;Cmd.pm';
$ApidOffset = $Application{&lt;name&gt;}{AppApidOffset};
	
foreach $cmd (keys %&lt;name&gt;Cmd) {
	if (int($fssbCmd{$cmd}{fixed_pattern}) &lt;= 15) {
		$opcode = sprintf("0%x",$&lt;name&gt;Cmd{$cmd}{fixed_pattern});
	} else {
		$opcode = sprintf("%x",$&lt;name&gt;Cmd{$cmd}{fixed_pattern});
	}
	$apid = sprintf("%x",$ApidBase+$ApidOffset);
	$cmdData{$cmd} = {opcode =&gt; "0x$opcode",
			apid =&gt; "0x$apid"};
};
&lt;/code&gt;
&lt;p&gt;Wherever you see &lt;code&gt;&lt;name&gt;&lt;/code&gt;, that indicates where the code differs each time. I have a bunch of files with the naming convention &lt;code&gt;&lt;name&gt;Cmd.pm&lt;/code&gt;, and each file contains a hash called &lt;code&gt;&lt;name&gt;Cmd&lt;/code&gt;. In each case, I need to get at the array in the file and do the exact same thing to it.&lt;/p&gt;
&lt;p&gt;So I have this code repeating right now, once for each file. I know there has to be a better way, but all I'm coming up with so far is naming a variable with a variable, which seems generally a bad idea, and I'm not too sure it would work anyway.&lt;/p&gt;
&lt;p&gt;So here I am, dear Monks - hoping someone knows a better way. A way that will work without repeating the same code 11 times. A way that won't induce heavy drinking and swearing in anyone working with this code after me.&lt;/p&gt;
&lt;p&gt;Also, here's an example of the type of data that is being sucked in from the &lt;code&gt;&lt;name&gt;Cmd.pm&lt;/code&gt; files:&lt;br /&gt;
&lt;code&gt;
%&lt;name&gt;Cmd = (
  'command_1' =&gt; {
    _discussion =&gt; '
       some text about command_1',
    description =&gt; '
       shorter text about command_1',
    safety_level =&gt; 'SAFE',
    fixed_pattern =&gt; '143',
    constraints =&gt; undef,
    build_types =&gt; [
      'FLIGHT'
    ],
    cmd_id =&gt; 250033,
    target =&gt; 'command_target',
    Command_maps =&gt; []
  },
  'command_2' =&gt; {
    _discussion =&gt; '
       some text about command_2',
    description =&gt; '
       shorter text about command_2',
    safety_level =&gt; 'SAFE',
    fixed_pattern =&gt; '144',
    constraints =&gt; undef,
    build_types =&gt; [
      'FLIGHT'
    ],
    cmd_id =&gt; 250014,
    target =&gt; 'command_target',
    Command_maps =&gt; []
  }
);
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;As always, THANK YOU!&lt;/P&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-826783"&gt;
Kaiti&lt;br /&gt;
Swiss Army Nerd
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
