Hash Dereference in Template Toolkit - perl

I've got a multi-dimensional hash that I'm trying to print out in a table. I can't get the referencing / dereferencing right.
I'm putting an excel spreadsheet into the hash and I want to print out the corresponding rows and columns in html and match the rows/columns of the spreadsheet (some of which are empty).
I'm using Perl Dancer and Template Toolkit. On the server side the hash works fine. print $big_table{$column}{$row}; on the server side and it prints the correct column and row with NO issues.
On the client side, the 0, 1, 2... are supposed to be the columns. Some columns are blank so I can't just print the contents.
The way it is now it prints ARRAY(0x3e5389c). I tried a different way and it printed HASH...
I know I've got some reference/dereference issues. Any advice would be welcome.
Server Side Code:
my %big_table = ();
# $cell->value() is the text ripped from the excel cell at that location
$big_table{$column}{$row} = $cell->value();
template 'index', { big_table => \%big_table };
Client Side:
<Table border="3">
<% FOREACH n IN big_table.0 %>
<TR><TD>&nbsp<% big_table.0.keys %>&nbsp<TD>&nbsp<% big_table.1.keys %>
&nbsp<TD>&nbsp<% big_table.2.keys %>&nbsp<TD>&nbsp<% big_table.3.keys %>
&nbsp <TD>&nbsp<% big_table.4.keys %>
&nbsp<TD>&nbsp<% big_table.5.keys %>&nbsp
<% END %>
</Table>
Thanks in advance!

Got it working.
Changed to an array. '$big_table[$col][$row] = $cell->value();' and populated a second array with all the row #'s.
The Client looks like
<% FOREACH r IN row_numbers %>
<TR><TD> &nbsp <% big_table.0.$r %> &nbsp <TD> &nbsp <% big_table.1.$r %>...
<% END %>
Works great but it's probably crazy in-effecient :(. The spreadsheet is 800 rows long so it's a 2nd array with 800 elements just to iterate over 'FOREACH' loop.

Related

show some data in koha ILS authority normal view

in opac-authoritiesdetail.pl
i add
$template->param( "tag_781_z" => $record->subfield('781','z') );
and in opac-auth-detail.tt
[% IF( tag_781_z ) %]
<span class="">
<span class="label">Geographic Subdivision
</span>
[% tag_781_z %]
</span>
[% END %]
the result
the first value only shown
but I add 3 value
If field 780$z is repeatable you must add value this field to array.
Regards

Why this line stops Sphinx search?

I use sanitizing from example: Barryhunter's
But when I use the line:
$q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/',' ',trim(strtolower($q)));
then Russian search don't works! Only English.
What the reason? How I should use sanitizing?
This is my piece:
<HTML>
<BODY>
<form action="" method="get">
<input name="q" size="40" value="<?php echo #$_GET['q']; ?>" />
<input type="submit" value="Search" />
</form>
<?php
require ( 'sphinxapi.php' );
$sphinx = new SphinxClient;
$sphinx->SetServer('ununtu', 9312);
$sphinx->open();
$sphinx->SetMatchMode (SPH_MATCH_EXTENDED);
$sphinx->setFieldWeights(array(
'title' => 10,
'content' => 5
));
$sphinx->SetRankingMode(PH_RANK_WORDCOUNT);
$sphinx->SetSortMode(SPH_SORT_RELEVANCE);
$sphinx->setLimits(0, 10, 200);
$sphinx->resetFilters();
$q = isset($_GET['q'])?$_GET['q']:'';
$q = preg_replace('/ OR /',' | ',$q);
// $q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/',' ',trim(strtolower($q)));
if(isset($_GET['q']) and strlen($_GET['q']) > 1)
{
$result = $sphinx->query($sphinx->escapeString($q), '*');
...
Assuming your input string is utf-encoded you use non-unicode preg_replace. Add 'u' in the end, e.g.:
$q = preg_replace('/[^\w~\|\(\)\^\$\?"\/=-]+/u',' ',trim(strtolower($q)));
Specifically that regex is stripping anything that is not a 'word' char, or a predefined list of syntax/punctuation chars.
The PREG definition of word (the \w ) is
A "word" character is any letter or digit or the underscore character,
that is, any character which can be part of a Perl "word". The
definition of letters and digits is controlled by PCRE's character
tables, and may vary if locale-specific matching is taking place. For
example, in the "fr" (French) locale, some character codes greater
than 128 are used for accented letters, and these are matched by \w.
http://php.net/manual/en/regexp.reference.escape.php
So possibly in English locale (or other western European for example), hence many Russian chars are not considered a word char, and stripped.
(if your pages are in UTF8, then may also need the /u as mentioned by other answer)

Using a variable to access hash keys in Template Toolkit

I have an array whose contents is
$VAR1 = {
'1' => 'May 05, 2011',
'0' => 'Jul 22, 2009',
'2' => 'Jun 13, 2012'
};
I am trying to display it in the catalyst template, the code is
[% x = 0 %]
[% FOREACH mortgage IN mortgages %]
<table width=40% border=1 cellspacing="0" cellpadding="10">
<tr>
<td>Date</td>
<td><b>[% dateformat.x %]</b></td>
</tr>
</table>
[% x = x+1 %]
[% END %]
The dateformat.x should display May 05, 2011 or Jul 22, 2009 or Jun 13, 2012 according to the value of x but the error is that it displays nothing. It shows a blank .
The error I think is that the key in the array is a string while the value of x that is used with the dateformat is numeric. If I add 0 or 1 with the dateformat then it displays correctly([% dateformat.0 %]).
[% dateformat.x %] looks in the dateformat hash for a key of x. To tell template toolkit that x is a variable, prefix it with $:
[% dateformat.$x %]
To access a hash entry using a key stored in another variable, prefix the key variable with '$' to have it interpolated before use (see Variable Interpolation).
I appreciate this question has already been asked and answered, but a handy alternative is the item() VMethod. This is particularly effective when you have hash keys that conflict with VMethods:
[%- SET myhash = { last => 'Blues', first => 'Elwood',
address => '1060 West Addison', city => 'Chicago' };
myhash.first; # doesn't do what you want,
# because first is a VMethod for 1st element in an array
myhash.item('first'); # displays "Elwood"
-%]
VMethods like first, last, size and sort are common traps for the unwary here.

sed/awk Capitallize everything between patterns and lowercase small words

I did find a way to capitalize the whole document, with both sed and awk, but how to do it, if I want to convert everything inside patterns from CAPS LOCK to Capital?
For example, I have an HTML file, and everything (multiple occurrences) between <b> and </b> has to be converted from TITLE to Title, and if possible making small words (1 ~ 2 letters) in lowercase.
From This:
<div id="1">
<div class="p"><b>THIS IS A RANDOM TITLE</b></div>
<table class="hugetable">
...
</table>
<div class="p"><b>THIS IS ANOTHER RANDOM TITLE</b></div>
<table class="hugetable">
...
</table>
...
</div>
To this:
<div id="1">
<div class="p"><b>This is a Random Title</b></div>
<table class="hugetable">
...
</table>
<div class="p"><b>This is Another Random Title</b></div>
<table class="hugetable">
...
</table>
...
</div>
This is not the most beautiful solution but I think it works:
sed -r -e '/<b>/ {s/( .)([^ ]*)/\1\L\2/g}' -e 's/<b>(.)/<b>\u\1/' -e '/<b>/ {s/(\b.{1,2}\b)/\L\1/g}' data
Explanation:
1st expression (-e): If a line contains <b>:
Then for each word which has a space in front of it, keep the space and the first (already capitalized) character (\1) and then convert all the following characters of the word to lower case (\L\2)
2nd expression (-e): The first word after <b> is still uncapitalized, so select the first character after the bold tag <b>(.) and replace it uppercased <b>\u\1
3rd expression (-e): Again if a line contains <b>:
Then select words of 1 or 2 characters in length \b.{1,2}\b and replace them lowercased \L\1

Can I use a Coffeescript `switch` block in eco?

Can I use a Coffeescript switch block in the eco templating engine? I tried a couple of variations, but I keep getting unexpected dedent errors.
Update: To appease the downvoters, here is what I expected to work
<% switch x : %>
<% when 1 : %>
one
<% end %>
<% when 2 : %>
two
<% end %>
<% end %>
But I get "Parse error on line 5: unexpected dedent"
ECO templates don't appear to support the switch statement.
The generated CoffeeScript code for your code is:
switch x
__out.push '\n'
when 1
__out.push '\n one\n'
__out.push '\n'
when 2
__out.push '\n two\n'
__out.push '\n'
The two __out.push '\n' lines after switch x and the end of the second when statement don't seem to allow this CoffeeScript snippet to compile into JavaScript.
Looking through the code, I couldn't figure out how to prevent those lines from being printed. This might be a good bug to report to the guys that make eco.
I'm only somewhat familiar with eco, but it seems as though it would just not be creating the right CS from that expression. Considering CS uses when x then y, I'm not sure you're getting that on compile.
You could try this instead:
<% switch x : %>
<% when 1 then: %>
one
<% end %>
<% when 2 then: %>
two
<% end %>
<% end %>