Template Toolkit Sort Hash
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID? <ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table> Thanks
Hi Richard, On Fri, 25 Sep 2015 13:40:39 -0500 Richard Reina <gatorreina@gmail.com> wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr>
1. You should put the "<% END %>" after the "</tr>" or otherwise the HTML won't be well-formed (even though it may still display correctly). 2. Please consider passing the Pats hash to the view/template as an already sorted array-reference (or as a different parameter to it that way) because there's a limit to how much complexity you should put in the template (even assuming it may be possible). Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ http://www.shlomifish.org/humour/bits/Can-I-SCO-Now/ - “Can I SCO Now?” Chuck Norris helps God help those that help themselves. — http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/ Please reply to list if it's a mailing list post - http://shlom.in/reply .
1. You should put the "<% END %>" after the "</tr>" or otherwise the HTML won't be well-formed (even though it may still display correctly).
Thank you for pointing that out. I will correct.
2. Please consider passing the Pats hash to the view/template as an already sorted array-reference (or as a different parameter to it that way) because there's a limit to how much complexity you should put in the template (even assuming it may be possible).
So I am learning.
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct? Anyway, you can pass "sort" the name of the key that you want sort on. <% FOREACH Pat IN Pats.sort('SNAME') %> <tr class="list-group-item"> <td width="70"><% Pats.SNAME %></td> <td width="75"><% Pats.ANAME %></td> <td width="35"><% Pats.SSN %></td> <td width="35"><% Pats.YR %></td> <td width="250"><% Pats.CHNAME %></td> <td width="550"><% Pats.DESCRIP %></td> </tr> <% END %> DAve... -- Dave Cross :: dave@dave.org.uk http://dave.org.uk/ @davorg
Hi Dave, On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave@dave.org.uk> wrote:
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct?
Anyway, you can pass "sort" the name of the key that you want sort on.
[KB] This is a very common problem asked previously and no solution found. It was advised to use array ref instead of hashref at previous mails. Now I tried your offer and see that it does not work. Values are unordered again. Richard, Did you try, does it work?
<% FOREACH Pat IN Pats.sort('SNAME') %> <tr class="list-group-item"> <td width="70"><% Pats.SNAME %></td> <td width="75"><% Pats.ANAME %></td> <td width="35"><% Pats.SSN %></td> <td width="35"><% Pats.YR %></td> <td width="250"><% Pats.CHNAME %></td> <td width="550"><% Pats.DESCRIP %></td> </tr> <% END %>
DAve...
-- Dave Cross :: dave@dave.org.uk http://dave.org.uk/ @davorg
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
On 26/09/15 19:25, Kadir Beyazlı wrote:
Hi Dave,
On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave@dave.org.uk <mailto:dave@dave.org.uk>> wrote:
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct?
Anyway, you can pass "sort" the name of the key that you want sort on.
[KB] This is a very common problem asked previously and no solution found. It was advised to use array ref instead of hashref at previous mails. Now I tried your offer and see that it does not work. Values are unordered again.
Damn, yes. There were a couple of typos in my solution. I didn't have time to test it. Sorry about that. Of course, I can't be sure until I know what your data structure really looks like, but this demonstrates a solution for the data structure that seems to make most sense. [% Pats = { id1 => { ID => 'id1', SNAME => 'ZZZ', ANAME => 'aname1', }, id2 => { ID => 'id2', SNAME => 'XXX', ANAME => 'aname2', }, id3 => { ID => 'id3', SNAME => 'YYY', ANAME => 'aname3', } } -%] Original: [% FOREACH id IN Pats.keys.sort -%] [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %] [% END -%] New: [% FOREACH Pat IN Pats.values.sort('SNAME') -%] [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %] [% END -%] Cheers, Dave...
On Sat, Sep 26, 2015 at 10:49 PM, Dave Cross <dave@dave.org.uk> wrote:
On 26/09/15 19:25, Kadir Beyazlı wrote:
Hi Dave,
On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave@dave.org.uk <mailto:dave@dave.org.uk>> wrote:
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct?
Anyway, you can pass "sort" the name of the key that you want sort on.
[KB] This is a very common problem asked previously and no solution found. It was advised to use array ref instead of hashref at previous mails. Now I tried your offer and see that it does not work. Values are unordered again.
Damn, yes. There were a couple of typos in my solution. I didn't have time to test it. Sorry about that.
Of course, I can't be sure until I know what your data structure really looks like,
[KB] The data Richard mentioned is a classic fetchall_hashref data as below: $Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ID is key of other attributtes. He asks if it id possible to sort by SNAME at above structure. I am using arrayref but this is common questin asked by beginners and no solution found
but this demonstrates a solution for the data structure that seems to make most sense.
[% Pats = { id1 => { ID => 'id1', SNAME => 'ZZZ', ANAME => 'aname1', }, id2 => { ID => 'id2', SNAME => 'XXX', ANAME => 'aname2', }, id3 => { ID => 'id3', SNAME => 'YYY', ANAME => 'aname3', } } -%] Original: [% FOREACH id IN Pats.keys.sort -%] [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %] [% END -%] New: [% FOREACH Pat IN Pats.values.sort('SNAME') -%] [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %] [% END -%]
Cheers,
Dave...
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal. First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: $Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ]; Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order. Hope it helps. Wbr, -- Kõike hääd, G
Hi WK, On Sun, Sep 27, 2015 at 3:13 PM, WK <wanradt@gmail.com> wrote:
2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal.
First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: [KB] I always use array ref if I need to order a database data because hash is unordered as you wrote and sort function sorts by key not by vaue (At perl code, it also sorts by value) at template toolkit. I tried to explain data structure Richard has trouble to Dave because Dave wrote a solution
Richard, I offer you using arrayref at template toolkit too. Template toolkit supplies reaching values by table column names unlike perl code (t Perl code, we ave to use index to reach array data) which is very useful
$Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ];
Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order.
Hope it helps.
Wbr, -- Kõike hääd,
G _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
Kadir, This will keep hash order, http://search.cpan.org/~chorny/Tie-IxHash-1.23/lib/Tie/IxHash.pm I only use it when I have to, but there is not too much of a slowdown, and I would guess your hashes are small anyway. I don't use Dancer myself, but follow its progress. Niels L On Sun, 2015-09-27 at 15:30 +0300, Kadir Beyazlı wrote:
Hi WK,
On Sun, Sep 27, 2015 at 3:13 PM, WK <wanradt@gmail.com> wrote:
2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal.
First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: [KB] I always use array ref if I need to order a database data because hash is unordered as you wrote and sort function sorts by key not by vaue (At perl code, it also sorts by value) at template toolkit. I tried to explain data structure Richard has trouble to Dave because Dave wrote a solution
Richard,
I offer you using arrayref at template toolkit too. Template toolkit supplies reaching values by table column names unlike perl code (t Perl code, we ave to use index to reach array data) which is very useful
$Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ];
Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order.
Hope it helps.
Wbr, -- Kõike hääd,
G _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hi Niels, On Sun, Sep 27, 2015 at 3:37 PM, Niels Larsen <niels@genomics.dk> wrote:
Kadir,
This will keep hash order,
http://search.cpan.org/~chorny/Tie-IxHash-1.23/lib/Tie/IxHash.pm
I only use it when I have to, but there is not too much of a slowdown, and I would guess your hashes are small anyway. I don't use Dancer myself, but follow its progress. [KB] Thanks, the solution Dave offered works at template toolkit also. Niels L
On Sun, 2015-09-27 at 15:30 +0300, Kadir Beyazlı wrote:
Hi WK,
On Sun, Sep 27, 2015 at 3:13 PM, WK <wanradt@gmail.com> wrote:
2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal.
First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: [KB] I always use array ref if I need to order a database data because hash is unordered as you wrote and sort function sorts by key not by vaue (At perl code, it also sorts by value) at template toolkit. I tried to explain data structure Richard has trouble to Dave because Dave wrote a solution
Richard,
I offer you using arrayref at template toolkit too. Template toolkit supplies reaching values by table column names unlike perl code (t Perl code, we ave to use index to reach array data) which is very useful
$Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ];
Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order.
Hope it helps.
Wbr, -- Kõike hääd,
G _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
Hi Richard, Did you see solution? I am writing incase you missed it. Below is the solution of the problem you asked solved by Dave Original: [% FOREACH id IN Pats.keys.sort -%] [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %] [% END -%] New: [% FOREACH Pat IN Pats.values.sort('SNAME') -%] [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %] [% END -%] On Sun, Sep 27, 2015 at 4:07 PM, Kadir Beyazlı <kadirbeyazli@gmail.com> wrote:
Hi Niels,
On Sun, Sep 27, 2015 at 3:37 PM, Niels Larsen <niels@genomics.dk> wrote:
Kadir,
This will keep hash order,
http://search.cpan.org/~chorny/Tie-IxHash-1.23/lib/Tie/IxHash.pm
I only use it when I have to, but there is not too much of a slowdown, and I would guess your hashes are small anyway. I don't use Dancer myself, but follow its progress. [KB] Thanks, the solution Dave offered works at template toolkit also. Niels L
On Sun, 2015-09-27 at 15:30 +0300, Kadir Beyazlı wrote:
Hi WK,
On Sun, Sep 27, 2015 at 3:13 PM, WK <wanradt@gmail.com> wrote:
2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal.
First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: [KB] I always use array ref if I need to order a database data because hash is unordered as you wrote and sort function sorts by key not by vaue (At perl code, it also sorts by value) at template toolkit. I tried to explain data structure Richard has trouble to Dave because Dave wrote a solution
Richard,
I offer you using arrayref at template toolkit too. Template toolkit supplies reaching values by table column names unlike perl code (t Perl code, we ave to use index to reach array data) which is very useful
$Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ];
Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order.
Hope it helps.
Wbr, -- Kõike hääd,
G _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
El 27 sept 2015, a las 7:30 AM, Kadir Beyazlı <kadirbeyazli@gmail.com> escribió:
Hi WK,
On Sun, Sep 27, 2015 at 3:13 PM, WK <wanradt@gmail.com> wrote: 2015-09-27 14:48 GMT+03:00 Kadir Beyazlı <kadirbeyazli@gmail.com>:
Of course, I can't be sure until I know what your data structure really looks like, [KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
When we need those elements be in particular order, sorted by any deeper value in hash tree, there is two main solutions for this goal.
First (and best for me). As hash can't maintain an order of elements, you provide different data structure in right order, namely arrayref, something like: [KB] I always use array ref if I need to order a database data because hash is unordered as you wrote and sort function sorts by key not by vaue (At perl code, it also sorts by value) at template toolkit. I tried to explain data structure Richard has trouble to Dave because Dave wrote a solution
Richard,
I offer you using arrayref at template toolkit too. Template toolkit supplies reaching values by table column names unlike perl code (t Perl code, we ave to use index to reach array data) which is very useful
$Pats = [ { 123 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 124 => { SNAME => Value, CHNAME => Value, ANAME => Value, } }, { 125 => { SNAME => Value, CHNAME => Value, ANAME => Value, } } ];
Other variant is to use some helper in your templating system, which provides map/reduce (Schwarzian transform) functionality. So you can feed in your hash and it gives back id-s in specified order.
Hope it helps.
Wbr, -- Kõike hääd,
G _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00 _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I think I am going to try to figure out how to use an array reference instead. Traveling so can't try it until I get back tomorrow. Thanks for all the replies.
On 27/09/15 16:34, Richard Reina wrote:
I think I am going to try to figure out how to use an array reference instead. Traveling so can't try it until I get back tomorrow.
You can, of course, try whatever approach you want. But my second solution will work. Dave... -- Dave Cross :: dave@dave.org.uk http://dave.org.uk/ @davorg
2015-09-27 11:24 GMT-05:00 Dave Cross <dave@dave.org.uk>:
On 27/09/15 16:34, Richard Reina wrote:
I think I am going to try to figure out how to use an array reference
instead. Traveling so can't try it until I get back tomorrow.
You can, of course, try whatever approach you want. But my second solution will work.
Dave...
--
Hi Dave, This did in fact work. Here is how I ended up incorporating your solution. <div class="well" style="max-height: 300px;overflow: auto;"> <ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH Pat IN Pats.values.sort('SNAME') -%> <tr class="list-group-item"> <td width="25"><% Pat.ID %> <td width="70"><% Pat.SNAME %> <td width="75"><% Pat.ANAME %> <td width="35"><% Pat.SSN %> <td width="35"><% Pat.YR %> <td width="250"><% Pat.CHNAME %> <td width="550"><% Pat.DESCRIP %> </tr> <% END -%> </ul> </table> </div> Thank you very much.
Hi Richard, some comments on your code: On Sun, 27 Sep 2015 17:59:42 -0500 Richard Reina <gatorreina@gmail.com> wrote:
2015-09-27 11:24 GMT-05:00 Dave Cross <dave@dave.org.uk>:
On 27/09/15 16:34, Richard Reina wrote:
I think I am going to try to figure out how to use an array reference
instead. Traveling so can't try it until I get back tomorrow.
You can, of course, try whatever approach you want. But my second solution will work.
Dave...
--
Hi Dave,
This did in fact work. Here is how I ended up incorporating your solution.
<div class="well" style="max-height: 300px;overflow: auto;"> <ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH Pat IN Pats.values.sort('SNAME') -%> <tr class="list-group-item"> <td width="25"><% Pat.ID %> <td width="70"><% Pat.SNAME %> <td width="75"><% Pat.ANAME %> <td width="35"><% Pat.SSN %> <td width="35"><% Pat.YR %> <td width="250"><% Pat.CHNAME %> <td width="550"><% Pat.DESCRIP %>
1. You're missing the closing tag - "</td>". 2. Are you sure you're avoiding https://en.wikipedia.org/wiki/Cross-site_scripting ? Also see http://perl-begin.org/tutorials/bad-elements/#code_and_markup_injection . Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Escape from GNU Autohell - http://www.shlomifish.org/open-source/anti/autohell/ By the time Chuck Norris found out who John Galt is, Summer Glau already had sex with him. — http://www.shlomifish.org/humour/bits/facts/Summer-Glau/ Please reply to list if it's a mailing list post - http://shlom.in/reply .
On 27/09/15 12:48, Kadir Beyazlı wrote:
On Sat, Sep 26, 2015 at 10:49 PM, Dave Cross <dave@dave.org.uk> wrote:
On 26/09/15 19:25, Kadir Beyazlı wrote:
Hi Dave,
On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave@dave.org.uk <mailto:dave@dave.org.uk>> wrote:
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is there a way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct?
Anyway, you can pass "sort" the name of the key that you want sort on.
[KB] This is a very common problem asked previously and no solution found. It was advised to use array ref instead of hashref at previous mails. Now I tried your offer and see that it does not work. Values are unordered again.
Damn, yes. There were a couple of typos in my solution. I didn't have time to test it. Sorry about that.
Of course, I can't be sure until I know what your data structure really looks like,
[KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
ID is key of other attributtes. He asks if it id possible to sort by SNAME at above structure. I am using arrayref but this is common questin asked by beginners and no solution found
My second solution (shown below) works fine in this situation. Dave...
but this demonstrates a solution for the data structure that seems to make most sense.
[% Pats = { id1 => { ID => 'id1', SNAME => 'ZZZ', ANAME => 'aname1', }, id2 => { ID => 'id2', SNAME => 'XXX', ANAME => 'aname2', }, id3 => { ID => 'id3', SNAME => 'YYY', ANAME => 'aname3', } } -%] Original: [% FOREACH id IN Pats.keys.sort -%] [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %] [% END -%] New: [% FOREACH Pat IN Pats.values.sort('SNAME') -%] [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %] [% END -%]
Cheers,
Dave...
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Dave Cross :: dave@dave.org.uk http://dave.org.uk/ @davorg
Hi Dave, On Sun, Sep 27, 2015 at 3:38 PM, Dave Cross <dave@dave.org.uk> wrote:
On 27/09/15 12:48, Kadir Beyazlı wrote:
On Sat, Sep 26, 2015 at 10:49 PM, Dave Cross <dave@dave.org.uk> wrote:
On 26/09/15 19:25, Kadir Beyazlı wrote:
Hi Dave,
On Sat, Sep 26, 2015 at 12:11 PM, Dave Cross <dave@dave.org.uk <mailto:dave@dave.org.uk>> wrote:
On 25/09/15 19:40, Richard Reina wrote:
I display a hash reference in a template. It works well but is
there a
way I can sort it by SNAME instead of ID?
<ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Pats.keys.sort %> <tr class="list-group-item"> <td width="70"><% Pats.$ID.SNAME %></td> <td width="75"><% Pats.$ID.ANAME %></td> <td width="35"><% Pats.$ID.SSN %></td> <td width="35"><% Pats.$ID.YR %></td> <td width="250"><% Pats.$ID.CHNAME %></td> <td width="550"><% Pats.$ID.DESCRIP %></td> <% END %> </tr> </ul> </table>
It looks like you have a hash of hashes. Is that correct?
Anyway, you can pass "sort" the name of the key that you want sort on. [KB] This is a very common problem asked previously and no solution found. It was advised to use array ref instead of hashref at previous mails. Now I tried your offer and see that it does not work. Values are unordered again.
Damn, yes. There were a couple of typos in my solution. I didn't have time to test it. Sorry about that.
Of course, I can't be sure until I know what your data structure really looks like,
[KB] The data Richard mentioned is a classic fetchall_hashref data as below:
$Pats = {ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, }, ID => { SNAME => Value, CHNAME => Value, ANAME => Value, } }
ID is key of other attributtes. He asks if it id possible to sort by SNAME at above structure. I am using arrayref but this is common questin asked by beginners and no solution found
My second solution (shown below) works fine in this situation.
[KB] You are right. I did not realize that the data structure you wrote at your 2nd mail is same with the data structure I wrote. It works. I had given up using hashref at template toolkit because of this problem and started using arrayref. But I can use now :)
Dave...
but this demonstrates a solution for the data structure that seems to make most sense.
[% Pats = { id1 => { ID => 'id1', SNAME => 'ZZZ', ANAME => 'aname1', }, id2 => { ID => 'id2', SNAME => 'XXX', ANAME => 'aname2', }, id3 => { ID => 'id3', SNAME => 'YYY', ANAME => 'aname3', } } -%] Original: [% FOREACH id IN Pats.keys.sort -%] [% id %] / [% Pats.$id.SNAME %] / [% Pats.$id.ANAME %] [% END -%] New: [% FOREACH Pat IN Pats.values.sort('SNAME') -%] [% Pat.ID %] / [% Pat.SNAME %] / [% Pat.ANAME %] [% END -%]
Cheers,
Dave...
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Dave Cross :: dave@dave.org.uk http://dave.org.uk/ @davorg _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Kadir Beyazlı Computer Engineer GSM : +90 535 821 50 00
participants (6)
-
Dave Cross -
Kadir Beyazlı -
Niels Larsen -
Richard Reina -
Shlomi Fish -
WK