[Petal] repeat through hash

Corey corey_s at qwest.net
Fri Apr 21 02:52:28 BST 2006


On Thursday 20 April 2006 17:29, Jonathan Vanasco wrote:
> On Apr 20, 2006, at 7:24 PM, Corey wrote:
> > Anyone in favor? Any pitholes or details I'm missing?
> 
> Well there's this:
> 	in Perl, Python and PHP (the TAL implementations I use ),  hashes  
> ( perl hashes, python dicts , php named arrays ) are unordered.  
> randomly showing items from a list, IMHO, is worthless and confusing.
> 

True.  And I agree that, from a presentation standpoint, randomly displaying
items from an unordered list is not always useful or sensical - however there
are certainly plenty of instances/use-cases where an ordered presentation 
is not in fact necessary, or where there truly is no criteria from which to sort 
by anyways. ( I'm currently writing an app in which this is the case. )

I have to point out though, that hashes/dicts/named-arrays are unordered
_internaly_, while arrays are merely ordered internally by subscript ( i.e.,
0 - N ) -- point being, for all intents and purposes:

$people_array = [
   {
      'firstname' => 'Benjamin',
      'lastname'  => 'Tucker'
      'id_num'    => '456a7'
   },
   {
      'firstname' => 'Pierre-Joseph',
      'lastname'  => 'Proudhon'
      'id_num'    => '765z4'
   }
];

... and

$people_hash = {
   '456a7' =>
      {
         'firstname' => 'Benjamin',
         'lastname'  => 'Tucker'
      },
   '765z4' =>
      {
         'firstname' => 'Pierre-Joseph',
         'lastname'  => 'Proudhon'
      }
};

... can both be said to be "unsorted", from a template user perspective. 
( assuming that $people_array wasn't _manually_ sorted by the programmer
in the order specified (i.e, first comes Tucker, then comes Proudhon) ).

So, while looping through $people_array, the template user would always 
get Tucker first, then Proudhon; and while looping through $people_hash, 
the user would not know for sure which order the results would be iterated - 
the difference is more or less the same: both "lists" were not filtered through
a purposed sort/order function.

So, appologies for the digression - but I'm attempting to illustrate that the issue
of sorting is quite separate from the issue of allowing Petal's 'repeat' statement 
to be implemented in a manner which allows transparent looping over hashes.

If the feature in question is:  

#1 - conformant to TAL spec
and
#2 - implementable in Petal
but
#3 - deemed usefull

... then I think it may be worth considering.

If either #1 _or_ #2 are definitely false, then there is no reason to continue.

If #1 and #2 are true, but #3 is debatable, then I think it would come down
simply to whoever got around to actually implementing it - and people could 
use it or not; but it would at least be available.


> to do what you want, it would just be going randomly through the hash
> 

That's ok, as it is not always necessary to loop over sorted lists -- in those 
cases, I feel that being able to loop/repeat over hashes would be quite helpfull 
and convienient - and could be implemented without parting from the TAL spec.


> i believe that in python you can map the repeat onto a generator or  
> iterator - i *think* you can figure out a way mimic that it in perl  
> using petal's method support
> 
> ie, your repeat loop calls a function that returns the next item in  
> whatever sequence, and petal stops doing that at the end
> 

This is definitely good food for thought - as I start using TAL/Petal more
and more.


> i tried to stab away at the petal source and try to get nested  
> variable to work.  i don't think its quite possible.
>

Bummer!

( Cool that you tried though. I was actually going to bring that up in another
post - hoping we/someone could fix that. )


> maybe some college student out there would want to do it through  
> google summer of code?  that would be awesome.
> 

Definitely.

Could you summarize what makes that feature so difficult?


CHeers,

Corey




More information about the Petal mailing list