Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving definition from subroutine to config hash

Status
Not open for further replies.

travs69

MIS
Dec 21, 2006
1,431
US
Currently I have a sub like this (scrubbed for public display)
Code:
sub decode_data {
	my $type = shift @_;
	my $date = shift @_;
	my (%return_data, $csv);
	if ($type =~ /TYPE1/i) {
		$csv = Text::CSV_XS->new({binary => 1, allow_loose_quotes => 1, allow_loose_escapes => 1, verbatim => 1, escape_char => '`'});
	}
	elsif ($type =~ /TYPE2/i) {
		$csv = Text::CSV_XS->new({binary => 1, allow_loose_quotes => 1, allow_loose_escapes => 1, verbatim => 1, escape_char => '/', quote_char => undef});
	}
	if ($csv->parse($line)) {
		my @col = $csv->fields();
		if ($type =~ /TYPE1/i){
			$return_data{val1} = $col[1];
			$return_data{val2} = $col[2];
			$return_data{val3} = $col[3];
		}
		#Yes this is here but not defined exactly above because the first regex catches it.  
		#Data is in the same place but sometime treated differently
		elsif ($type =~ /TYPE21/i){
			$return_data{val1} = $col[5];
			$return_data{val2} = uc $col[6];
			$return_data{val3} = lc $col[7];
		}
		elsif ($type =~ /TYPE2/i){
			$return_data{val1} = $col[5];
			$return_data{val2} = $col[6];
			$return_data{val3} = $col[7];
		}
	}
	else {
		$return_data{status} = 'fail';
	}
	return(%return_data);
}
I'm going to be getting a few more types and it seems silly to just keep building out that if statement (in the real code I'm setting a few hundred value plus doing a lot of calculations)

I was thinking something like this would be better but wanted to see if others had a better idea
Code:
%config = ( 'TYPE1' => {
				'val1' = 1,
				'val2' = 2,
				'val3' = 3,
			},
			'TYPE2' => {
				'val1' = 5,
				'val2' = 6,
				'val3' = 7,
			},
		)
$return_data{val1} = $col[$config{$type}{val1}];
$return_data{val2} = $col[$config{$type}{val2}];
$return_data{val3} = $col[$config{$type}{val3}];
or even
Code:
for my $key (keys %{$config{$type}}){
 $return_data{$key} = $col[$config{$type}{$key}];
}
I would have to of course work out a few issues but I definitely don't want to be maintaining 5+ if statements that are almost identical and are hundreds of lines long every time I need a new values. Mainly I just wanted to see what kind of suggestions come up that I wouldn't have though about.

Hopefully the explanation is ok!
[afro2]
Thanks!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You could always have an array of hashes rather than a hash of hashes ;-) But that would only work if the type numbers matched the indexing [idea] - a config file might work, but you would still need to parse the file for the definitions.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top