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

Ron Phipps interchange-users@interchange.redhat.com
Fri Apr 19 02:58:01 2002


> On Behalf Of Stefan Hornburg (Racke)
> 
> "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

Thanks Racke!  Your method is definitely shorter and appears that it
will work in a usertag as well.  I sure could use a dump of all the
gurus Perl knowledge right about now, have you guys perfected that
routine yet? ;)

Take it easy!
-Ron