[ic] Order of columns for hashref returned from query?

Stefan Hornburg Racke interchange-users@interchange.redhat.com
Thu Apr 18 04:18:02 2002


"Ron Phipps" <rphipps@reliant-solutions.com> writes:


[...]

> 
> While I was relaxing it hit me, why not just split on the comma in my
> available_breaks and then loop over those values and key into the hash
> to see if the there is a price break.  Below is the code that I came up
> with:
> 
> 		[seti tmp_code][item-code][/seti]
> 		[perl tables=pricing]
> 			my $out = "pricing:price_group,";
> 			my $code = $Scratch->{tmp_code};
> 			my $available_breaks =
> "q2,q3,q4,q5,q6,q7,q8,q9,q10,q12,q15,q25,q50,q100";
> 			my $break;
> 			my $sql = "SELECT " . $available_breaks . " FROM
> pricing WHERE sku ='" . $code . "';";	
> 			my $sql_results = $Tag->query( {     sql =>
> $sql, hashref => 'price_results' } );
> 			my $hash = $sql_results->[0];
> 			my @breaks = split (/,/, $available_breaks);
> 			foreach $break (@breaks) {
> 				if ($$hash{$break} ne '') {
> 					$out .= "$break,";
> 				}
> 			}
> 			return $out;
> 		[/perl]

Please note that the following code is shorter and more efficient
(but untested).
It is the beauty of Interchange that you can access all DB methods
directly from Perl:

$set = $Db{pricing}->query($sql);
$out = join(',', grep {$_ ne ''} @{$set->[0]});

Ciao
        Racke

-- 
Think of it !

For projects and other business stuff please refer to COBOLT NetServices
(URL: http://www.cobolt.net; Email: info@cobolt.net; Phone: 0041-1-3884400)