Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Splitting a hashref into hashrefs

by legLess (Hermit)
on Mar 20, 2003 at 08:19 UTC ( [id://244548]=note: print w/replies, xml ) Need Help??


in reply to Re: Splitting a hashref into hashrefs
in thread Splitting a hashref into hashrefs

One of my explicit goals is to make the HTML templates as easy to use as possible. I hold that this template:
<p>Org id: [% org.id %]</p> <p>Org name: [% org.name %]</p> <p>Group id: [% group.id %]</p> <p>Group name: [% group.name %]</p>
...is more intuitive and easier to use than this one, which is what I need if I use a single hashref:
<p>Org id: [% hashref.org_id %]</p> <p>Org name: [% hashref.org_name %]</p> <p>Group id: [% hashref.group_id %]</p> <p>Group name: [% hashref.group_name %]</p>
My other goal is to learn a few other ways of doing this, perhaps even a cool way to split a hashref into two hashrefs.

Most of this is copied from my post; perhaps you should have read it more carefully?
--
man with no legs, inc.

Replies are listed 'Best First'.
Re: Re: Re: Splitting a hashref into hashrefs
by perrin (Chancellor) on Mar 20, 2003 at 17:21 UTC
    I read it and understood; I just don't agree with your ideas about data structures for templating. I believe you should only nest your data structures when the data actually requires it, which is not the case here. If I were doing this, I would get rid of that top hashref and set it up like this:
    <p>Org id: [% org_id %]</p> <p>Org name: [% org_name %]</p> <p>Group id: [% group_id %]</p> <p>Group name: [% group_name %]</p>
    I would only use another level below this if there are multiple values. For example, if there are multiple groups per organization, I would represent groups as an array of hashes, so I could make a template like this:
    <p>Org id: [% org_id %]</p> <p>Org name: [% org_name %]</p> [% FOREACH group = groups %] <p>Group id: [% group.id %]</p> <p>Group name: [% group.name %]</p> [% END %]
      Thanks for the additional reply, and for being a bit more explicit in your disagreement. We'll have to agree to disagree, I think.

      There are many places in the templates where I do have multiple items and do require Template::Toolkit's dot.notation. I think it's better (and easier for the template maintainer) to be consistent by always using hashes than to switch between hashes and scalars.

      The point of my post wasn't to argue about my templating (although this is fine, really, and has given me some additional ideas to chew on), but to find a way to make the code do what I wanted. So far no one's touched upon my original question as indicated by my title; I'll try again.
      --
      man with no legs, inc.
        It looked to me like submersible_toaster answered your question. There is no way to get two hashrefs out of a single DBI query, so you have to divide it up later. Hash slices are the cleanest way to divide up a hash, as he shows in his example. They are the hash version of saying @array[0,1].

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://244548]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found