[ic] [query] hashref into scratches... this should work me thinks

Peter peter at pajamian.dhs.org
Fri Dec 3 07:04:54 UTC 2010

On 03/12/10 19:19, Paul Jordan wrote:
> Thanks Peter, that's pretty handy/cool. Actually, my email was simplified for brevity - I am doing more complex joins. I was working on Gerts suggestion, but I was getting:
> ...Safe: Not a HASH reference at (tag 'perl') line 3.
>>   my $bar = $Tmp->{foo};
>>   while (my ($k,$v) = each %$bar) { $Tag->tmp($k, $v); };
> So I have been playing with mutations of that, googling things, but in the end I'm 50% guessing 50% learning :-) I think I got this to work though:
>         [perl]
>             my $bar = $Tmp->{foo};
>             foreach my $row (@$bar) {
>                 while (my ($k,$v) = each(%$row)) { $Tag->tmp($k, $v); }
>             }
>         [/perl]

That would be for a multi-row query, but the problem with that is that
the fields will be the same for each query, so for each row you will be
setting the same scratch variables.  this means that each successive row
will override the last.  If you will just have one row returned from the
query then there's no need to loop through with a foreach loop like
that, you can just take $bar->[0] and get the first (and only) row.

> But that is literally [tmp] right? So i'll need to do something like ..$Scratch->{$k} = $v;

Right, for a complex query that returns a single row you can do
something like this:

[perl mytable]
	my $db = $Db{mytable};
	my $results = $db->query({sql => qq{SELECT ...}, hashref => 1});
	@{$Scratch}{keys %{$results->[0]}} = values %{$results->[0]};


More information about the interchange-users mailing list