[Templates] Getting keys from a hash

Karl Dane karl at rince.net
Tue Feb 26 11:17:44 GMT 2008


Right! So - there's something REALLY weird going on here...

To answer Simon, cgihash is from perl's CGI lib: my $cgi = new CGI; my 
$cgihash = $cgi->Vars;

Shouldn't be anything odd about it. But obviously it's behaving 
differently for some reason.

New example. cgihash continues to act as before, but cgihash2 works 
fine... :

cgihash:

should tell me this is a hash:
[% cgihash %]

dump out the contents of the hash:
[% USE Dumper %]
[% Dumper.dump(cgihash) %]

Now give me the keys:
[% FOREACH key = cgihash.keys %]
  [% key %]
[% END %]

cgihash2:

[%
  cgihash2 = {
             'a' => 'newentry',
             'newbooking' => '1',
             'day' => '06',
             'monthdigit' => '02',
             'year' => '2008',
             'room' => '9',
             'gender' => 'male',
             'beds' => '3',
             'nights' => '2',
             'submit' => 'Book'
           };

  %]

dump out the contents of the hash:
[% USE Dumper %]
[% Dumper.dump(cgihash2) %]

Now give me the keys:
[% FOREACH key = cgihash2.keys %]
  [% key %]
[% END %]


[[ TEMPLATE ENDS ]]

---
Output:

   cgihash:

should tell me this is a hash:
HASH(0x8830200)

dump out the contents of the hash:

$VAR1 = {
           'a' => 'newentry',
           'newbooking' => '1',
           'day' => '06',
           'monthdigit' => '02',
           'year' => '2008',
           'form_FirstName' => 'Charles ',
           'form_Name' => 'Bronson',
           'form_Address1' => 'home',
           'form_Address2' => '',
           'form_Address3' => '',
           'form_Town' => 'Ambleside',
           'form_County' => '',
           'form_Postcode' => '',
           'form_Country' => 'UK',
           'form_Email' => 'david at amblesidebackpackers.co.uk',
           'form_TelephoneNo' => '',
           'room' => '9',
           'gender' => 'male',
           'beds' => '3',
           'nights' => '2',
           'submit' => 'Book'
         };


Now give me the keys:


cgihash2:



dump out the contents of the hash:

$VAR1 = {
           'monthdigit' => '02',
           'a' => 'newentry',
           'nights' => '2',
           'submit' => 'Book',
           'room' => '9',
           'newbooking' => '1',
           'beds' => '3',
           'day' => '06',
           'year' => '2008',
           'gender' => 'male'
         };


Now give me the keys:

  monthdigit

  a

  nights

  submit

  room

  newbooking

  beds

  day

  year

  gender



[[ TEMPLATE ENDS ]]





Clinton Gormley wrote:
> On Tue, 2008-02-26 at 11:00 +0000, Karl Dane wrote:
>> I guess my example wasn't a very good one - I just wanted to demonstrate 
>> that I was getting _nothing_ when using the keys method. If it had 
>> worked as expected, then it should have returned something like 
>> 'ARRAY(0x8a8ca58)'
> 
> ... which is what I get when I try it on my system, with this code:
> 
> [%
> 
> cgihash = {
>            'a' => 'newentry',
>            'newbooking' => '1',
>            'day' => '06',
>            'monthdigit' => '02',
>            'year' => '2008',
>            'room' => '9',
>            'gender' => 'male',
>            'beds' => '3',
>            'nights' => '2',
>            'submit' => 'Book'
>          };
> 
> %]
> 
> should tell me this is a hash:
> [% cgihash %]
> 
> dump out the contents of the hash:
> [% USE Dumper %]
> [% Dumper.dump(cgihash) %]
> 
> Now give me the keys:
> [% cgihash.keys %]
> 
> [[ TEMPLATE ENDS ]]
> ------------------------------
> OUTPUT:
> 
> should tell me this is a hash:
> HASH(0x287c4a0)
> 
> dump out the contents of the hash:
> 
> $VAR1 = {
>           'monthdigit' => '02',
>           'a' => 'newentry',
>           'nights' => '2',
>           'submit' => 'Book',
>           'room' => '9',
>           'newbooking' => '1',
>           'beds' => '3',
>           'day' => '06',
>           'year' => '2008',
>           'gender' => 'male'
>         };
> 
> 
> Now give me the keys:
> ARRAY(0x28c7170)
> 
> [[ TEMPLATE ENDS ]]



More information about the templates mailing list