Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Template, sorting an array with only one member (code not words)

by Anonymous Monk
on Oct 20, 2014 at 10:56 UTC ( [id://1104413]=note: print w/replies, xml ) Need Help??


in reply to Template, sorting an array with only one member

Code talks louder than words :)
$ cat sort-list.tt2 [% MACRO blocker BLOCK %] [% FOREACH element IN array.sort('name') %] [% element.name %] [%- END -%] [%- END -%] [%- SET array = [ { name => 'foo', value => 1 }, { name => 'bar', value => 2 }, { name => 'baz', value => 3 } ]; blocker %] [%- SET array = [ { name => 'bar', value => 2 }, ]; blocker %] $ tpage sort-list.tt2 bar baz foo bar

My code says it works ... so what does your code say?

  • Comment on Re: Template, sorting an array with only one member (code not words)
  • Download Code

Replies are listed 'Best First'.
Re^2: Template, sorting an array with only one member (code not words)
by FloydATC (Deacon) on Oct 20, 2014 at 11:07 UTC

    I get the same result if I define the array within the template. If the array is served as a reference through Template::process() then it doesn't work as expected.

    -- FloydATC

    Time flies when you don't know what you're doing

      I get the same result if I define the array within the template. If the array is served as a reference through Template::process() then it doesn't work as expected.

      Thats just more words :) but I'm glad to see choroba got you to play ;) and you figured it out

Re^2: Template, sorting an array with only one member
by Anonymous Monk on Oct 20, 2014 at 11:05 UTC

    You must be giving it a hash instead of an array ... don't know how you managed to write that :)

    [% MACRO blocker BLOCK %] [% FOREACH element IN array.sort('name') %] [% element.name %] [%- END -%] [%- END -%] [%- SET array = [ { name => 'foo', value => 1 }, { name => 'bar', value => 2 }, { name => 'baz', value => 3 } ]; blocker; %] [%- SET array = [ { name => 'bar', value => 2 }, ]; blocker; %] [%- ## A HASH IS NOT AN ARRAY OF HASHES SET array = { name => 'bar', value => 2 }; blocker; %]

    yadayadayada

    * bar * baz * foo * bar * *

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found