<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2627" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>So, I ended up writing a little modifier, because the case changed
slightly. </DIV>
<DIV> </DIV>
<DIV>I want to use $variable to access a hash element such as:</DIV>
<DIV>
<DIV>$hash->{$variable}
<DIV>*** and ***</DIV>
<DIV>$hash->{$variable}->{'value'}</DIV>
<DIV> </DIV>
<DIV>So I wrote the following Modifier called
lookup: </DIV></DIV></DIV>
<DIV><PRE>$Petal::Hash::MODIFIERS->{'lookup:'} = sub {
my $hash = shift;
my $args = shift;
my @args = split(/\s+/, $args);
##the first is the hash name
my $the_hash = $hash->fetch ($args[0]);
my $param = $hash->fetch ($args[1]);
if($args[2])
{
return $the_hash->{$param}->{$args[2]};
}
else
{
return $the_hash->{$param};
}
};
</PRE></DIV>
<DIV>This is used in the code like:</DIV>
<DIV> </DIV>
<DIV><span tal:replace="lookup: the_hash variable field_name">replace
me</span></DIV>
<DIV> </DIV>
<DIV>and it works nicely</DIV>
<DIV> </DIV>
<DIV>I know I have no real validation built in yet, but that aside, any
comments?</DIV>
<DIV> </DIV>
<DIV>Regards</DIV>
<DIV> </DIV>
<DIV>Simon</DIV>
<DIV> </DIV>
<DIV> </DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=simonmcc@nortelnetworks.com
href="mailto:simonmcc@nortelnetworks.com">simonmcc@nortelnetworks.com</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=petal@lists.webarch.co.uk
href="mailto:petal@lists.webarch.co.uk">Petal Mailing List</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Friday, April 29, 2005 1:38
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [Petal] Accessing variable hash
elements</DIV>
<DIV><BR></DIV>
<DIV><FONT face=Arial size=2>Ok, I know this must be a stupid question, I
probably just need more caffeine but.....</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I need to access a variable hash element
$user{$group}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>if I have a Hash as : </FONT></DIV>
<DIV><FONT face=Arial size=2>my %user = ( name => 'Simon', group1 =>
'1', group3 => '1') ;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>and an array like :</FONT></DIV>
<DIV><FONT face=Arial size=2>my @groups = (qw/group1 group2 group3/
);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>and I want to do:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><td tal:repeat="group groups"></FONT></DIV>
<DIV><FONT face=Arial size=2> <span tal:content="user
group">is the user in the group</span></FONT></DIV>
<DIV><FONT face=Arial size=2></td></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I know this last bit is incorrect, but there must
be a simple way of accessing $user{$group}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Simon</FONT></DIV></BLOCKQUOTE></BODY></HTML>