Re: string to array? and geting items out of array in tt

Tom Molesworth <tom-UE50auxAeIt4LjgWlUEQQwC/[email protected]> Sun, 28 Oct 2012 18:19:43 +0000
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
Hi there,

On 28/10/12 17:28, Rajeev Prasad wrote:
> Hello,
>
> I am getting few columns from my table.
>
> itemid, will not be blank
> itemprop_comment, may be blank
> item_prop, will not be blank
>
> I am thinking to store it in an hash as follows...
>
> using a loop...
>
> while ($record = $record_rs->next ()){
>      my $itemid = $record->get_column("itemid");
>      my $itemdetail = $record->get_column("itemprop_comment")."\|".$record->get_column("item_prop");
>      $tmp_hash->{$itemid} = "$itemdetail";
> };

You could store as a hashref using this instead (I'd suggest renaming 
$tmp_hash to something more descriptive though):

$tmp_hash->{$itemid}{$_} = $record->get_column($_) for 
qw(itemprop_comment item_prop);

then inside this loop:

>          [%- FOREACH itemid IN itemlist.keys.sort -%]
>              itemid=[% itemid %] item_prop_comment=[%- itemlist.$itemid.replace('\|.*$','') -%]  item_property=[%- itemlist.$itemid.replace('.*\|','') -%] <br>
>          [% END -%]

do this instead:

itemid=[% itemid %] item_prop_comment=[% 
itemlist.item($itemid).item_prop_comment %] item_property=[% 
itemlist.item($itemid).item_prop %]

although I suspect that you'd need to put quotes around the values 
otherwise an item_prop_comment like "... item_property=something" could 
be ambiguous.

The documentation on Perl references and data structures may help - 
perldoc perldsc, perldoc perlreftut

cheers,

Tom