Richard,
I finally understand why I why I was getting inconsistent results.
I misunderstood what .sort was trying to accomplish. I am trying to sort on
the Hash keys, not the Hash values and it doesn't appear there is that
functionality currently in the system. To add that functionality I went
into Stash.pm and added a line at line 94:
'sortkeys' => sub { [ sort keys %{ $_[0] } ] },
and now am able to sort my hashes based on keys.
Katie Kee
-----Original Message-----
From: Richard Tietjen [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 14, 2002 6:25 AM
To: [EMAIL PROTECTED]
Cc: Kee, Katherine J
Subject: RE: [Templates] sort and nsort
> Example Data
> Bldg: EMSL, Contains Rooms 1224, 1335, 1423
> Bldg: 2400STV, contains rooms 1232, 1246, Aqua_Lab
> Bldg: 331, contains rooms Comp_Lab, 131, 234
> I would like to see the report display the data like this (the bldg names
> and room names can be numeric, alpha, or a combination. I want the sort
> alpha):
> 2400STV 1232
> 2400STV 1246
> 2400STV Aqua_Lab
> 331 131
> 331 234
> 331 Comp_Lab
> EMSL 1224
> EMSL 1335
> EMSL 1423
> Sometimes it has it in the correct order, but some times it will display
331
> then 2400STV, then EMSL, or any combination of those three in any order,
> plus any combination of the rooms.
> The data structure is built like this:
> %t_data = (
> EMSL => {
> 1224 => {title => "value", csm => "value"},
> 1335 => {title => "value", csm => "value"},
> 1423 => {title => "value", csm => "value"}
> },
> 331 => {
> Comp_Lab => {title => "value", csm => "value"},
> 131 => {title => "value", csm => "value"},
> 234 => {title => "value", csm => "value"}
> },
> 2400STV => {
> 1232 => {title => "value", csm => "value"},
> 1246 => {title => "value", csm => "value"},
> Aqua_Lab => {title => "value", csm => "value"}
> };
> Richard,
> You suggested that I do the following:
> You might change the t_data building and room structures to a lists of
> hash refs, and then you could pass the key parameter to sort and get
> back a list of records.
> sort, nsort
> Returns the items in alpha (sort) or numerical (nsort) order.
> [% library = books.sort %]
> Where the list contains hash array references, a hash key can be
> passed as an argument to specify the sort field.
> [% library = books.sort('author') %]
> then you'd have cleaner code, like this kinda:
> [% FOREACH bldg = t_data.sort('no') %]
> [% FOREACH room = bldg.sort('no') %]
> <B>[% bldg.no %] [% room.no %]</B>
> [% room.title %] ([% room.csm %])
> [% END %]
> [% END %]
> I am not entirely sure what you mean, or how I would map my existing data
to
> fit that criteria.
> Thanks for all your help.
> Katie Kee
Dear Katie Kee,
Here's my answer to the part of the email addressed to me, posted back
to the group. It demonstrates another way to code the data that you
can adjust to true perl syntax if you want to pass it into a template
as before, but it's easier to demonstrate this way. The sorting seems
consistently the way you'd expect.
[% bildings = [
{ no => 'EMSL',
rooms = [
{no => 1224, title => "value", csm => "value"},
{no => 1335, title => "value", csm => "value"},
{no => 1423, title => "value", csm => "value"},
]},
{ no = 331,
rooms => [
{no => 'Comp_Lab', title => "value", csm => "value"},
{no => 131, title => "value", csm => "value"},
{no => 234, title => "value", csm => "value"},
]},
{ no => '2400STV',
rooms => [
{no =>'1232', title => "value", csm => "value"},
{no => '1246', title => "value", csm => "value"},
{ no => 'Aqua_Lab', title => "value", csm => "value"},
]},
] %]
[% FOREACH b=bildings.sort('no') %]
[% FOREACH room = b.rooms.sort('no') %]
<B>[% b.no %] [% room.no %]</B>
[% room.title %] ([% room.csm %])
[% END %]
[% END %]
==>>
tpage /home/rtietjen/xxxxx.tt
<B>2400STV 1232</B>
value (value)
<B>2400STV 1246</B>
value (value)
<B>2400STV Aqua_Lab</B>
value (value)
<B>331 131</B>
value (value)
<B>331 234</B>
value (value)
<B>331 Comp_Lab</B>
value (value)
<B>EMSL 1224</B>
value (value)
<B>EMSL 1335</B>
value (value)
<B>EMSL 1423</B>
value (value)
> - -----Original Message-----
> From: Mark Mills [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, February 13, 2002 1:46 PM
> To: Kee, Katherine J
> Subject: RE: [Templates] sort and nsort
> > I am trying to sort a hash using the sort method and getting
> > inconsistent results.
> What aren't you getting that you expect? Based on the template names it
> looks to me that you have building numbers as hash keys. Do you want it
> sorted on a building name that isn't the number? You might want to sort
> using .nsort if you really do want them sorted numerically.
> Again, the same with rooms, you say room names but the only thing I see
are
> room numbers and room titles. If you want the room numbers to sort right
you
> should probably use .nsort again.
> If that doesn't help, can you show us just a little data? 2 buildings with
2
> or 3 rooms each, with the output you got and the output you want?
-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-
Richard Tietjen <[EMAIL PROTECTED]> www.pobox.com/~rdtietjen
"Irony is what they make two-edged swords from."
-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-