template toolkit .. one more kwestion
OK .. this ones driving me dippy .. I'm playing with Template::Toolkit with a bit of MySQL and the usual things ... so I wrote a few functions that retreive lists of err stuff. for speed i use dbi's fetchall_arrayref which returns a reference to an array of references to arrays ...uh huh I dereference it and pass the resulting array back out to my template for use in a [% FOREACH item = getItemList(category) %] because the array is full of references to arrays I then access each itemid as [% item.0 %] this works fine if the function returns a list with more than one thing in it .. if there is only one thing in the array I get a 'dont know how to access [ 3 ].0 if for example item 3 is returned. if I access the result as just plain [% item %] it works fine if there is one item in the list .. and returns lots of ARRAY0x1241234 if there is more than one ... its almost as if someone has infected it with a variant of sub::approx and its making some wild guesses about what its supposed to be doing and guessing different things in different situations .. either way its driving me dippy. any ideas on how to tell the thing to stop trying to second guess what I want or guess right each time? -- Robin Szemeti The box said "requires windows 95 or better" So I installed Linux!
Re: template toolkit .. one more kwestion
In message [EMAIL PROTECTED], Robin Szemeti [EMAIL PROTECTED] writes: this works fine if the function returns a list with more than one thing in it .. if there is only one thing in the array I get a 'dont know how to access [ 3 ].0 if for example item 3 is returned. if I access the result as just plain [% item %] it works fine if there is one item in the list .. and returns lots of ARRAY0x1241234 if there is more than one ... Sounds like your dereferencing function may be returning a SCALAR instead of an ARRAY when there's only one result? -- rob partington % [EMAIL PROTECTED] % http://lynx.browser.org/
Re: template toolkit .. one more kwestion
On Mon, 29 Jan 2001, you wrote: In message [EMAIL PROTECTED], Robin Szemeti [EMAIL PROTECTED] writes: this works fine if the function returns a list with more than one thing in it .. if there is only one thing in the array I get a 'dont know how to access [ 3 ].0 if for example item 3 is returned. if I access the result as just plain [% item %] it works fine if there is one item in the list .. and returns lots of ARRAY0x1241234 if there is more than one ... Sounds like your dereferencing function may be returning a SCALAR instead of an ARRAY when there's only one result? could be ... I had : my($data)=$sth-fetchall_arrayref; return @{$data}; which I thought should return a flat list of references to arrays ... ?? nope? -- Robin Szemeti The box said "requires windows 95 or better" So I installed Linux!
Re: template toolkit .. one more kwestion solved
On Mon, 29 Jan 2001, you wrote: Sounds like your dereferencing function may be returning a SCALAR instead of an ARRAY when there's only one result? could be ... I had : my($data)=$sth-fetchall_arrayref; return @{$data}; which I thought should return a flat list of references to arrays ... ?? nope? so .. after a quick fiddle .. It seem T::T automagically assumes that if you pass it a reference to something then you probably didn;t want that, you prolly wanted the thingy itself so it dereferences it for you.. it will only do this down one level. so by passing it a list of references it sees a list .. does a foreach down them and then sees what it gets (in this case an array ref ) .. it dereferences that and bingo you can now access the row of stuff. however when the list jsut contains 1 item, it sees a reference to an array ( whicj is the row) and then proceeds to do a foreach over the elements of the row .. by simply not passing it the thing in list format but passing the plain reference (ie return $data instead of @{$data}) it always gets an arrayref and works for both cases of 1 or n items in the array. solved .. it just took me a while to twig what was going on. -- Robin Szemeti The box said "requires windows 95 or better" So I installed Linux!
Re: template toolkit .. one more kwestion solved
On Mon, Jan 29, 2001 at 05:31:52PM +0100, Philip Newton wrote: Reminds me of fits I had when doing Vignette/Tcl with lists of lists that I passed to another template with HTTP POST. When the list of lists contained only one element, it didn't wrap that list in extra {} so the foreach say a list of lists all right -- but the "sublists" were only one element long (since {} are optional around lists if there are not spaces). Caused me a Try creating a JavaScript array of length one whose first element is initialised to 1. Aaargh. [1] Paul [1] # These numbers get turned into a JavaScript array later on that # looks like new Array (1,1,0); Owing to JavaScript1.0 braindeadedness # new Array (1) is interpreted as "created a one element list" not # "create a one element list with [0]=1". However this is achievable # if the number is given as a string like new Array("1"); @time_names = sort {$a = $b} keys %time_names; foreach my $i_list (keys %time_cache) { $time_cache{$i_list} = join ',', map {$time_cache{$i_list}{$_} ? 1 : 0} @time_names; $time_cache{$i_list} =~ s/^(.)$/"$1"/; }