Then I assume you know at the time that the various values need to
be shown which variables are involved. I suggest that you think of
your "reference hash" as a simple perl format string and use sprintf
or printf to use the pre-determined format string to produce the
required output. Perhaps I'm assuming too much. Do you know the
order and identity of the variables to be shown? If not then an approach
that you could take would be that the information stored in the
reference hash would be an entire sprintf or printf or prehaps
just a plain print sent with the variables escaped as you are
currently doing. Then all you have to do is to eval() the hash value.
For example if:

hashfmt{'1'} = "abc, 123, \$hash{\$x}{\$y}{'value'}, xyz, 345,
 \$hash{\$x}{\$y}{'value2'}" ;


Then all you would do is:

eval ("print $hashfmt{'1')");

Or if you really wanted to process the individual items separatly
you sould have code like:

    $count=1;
    foreach $item (@values) {
             if ($count % 3 == 0) {
                $_=$item;
                eval("\$item=$_;");
             }
             print "Item = [$item]\n" :
             $count++ ;
     }

*** [EMAIL PROTECTED] <Carl Jolley>
**** All opinions are my own and not necessarily those of my employer ****

On Tue, 4 Dec 2001, Ed DeBus wrote:

>
> Thanks all for the responses.  I think I may need to explain the code
> further.
>
> First, and most important, let's call the reference hash a "formatting" hash
> instead....this hash and it's values has nothing to do with perl references.
>
> I think the problem I'm having is that the formatting hash is defined
> immediately when the scripts starts, and it's format never changes, however,
> the variables (hash values) represented by the formatting hash constantly
> change.  If the variables represented in the formatting hash exist prior to
> the hashes keys/values being definded, then of course this works fine.
> However once on of the variables "refererneced" (not a perl reference) in
> the format hash change, that does not force the value of that variable in
> the format hash to change.
>
> Yes, I could refer to the variables I want directly when I need them.  The
> purpose for this is to be able to build a template that creates the
> formatted message I need when I need it (there are several hundred potential
> unique messages....hence the desire to build them dynamically from a
> template).
>
> Thanks!
>
>
> Ed DeBus
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Ron
> Hartikka
> Sent: Tuesday, December 04, 2001 8:29 AM
> To: Ed DeBus; [EMAIL PROTECTED]
> Subject: RE: dynamic variable evaluation
>
>
> You took some extra references; I removed some backslashes: seem method 'a'
> below.
>
> (If you really had to convert the substring \$hash{\$x}{$y}{'value'} to a
> reference, you'd need to use eval()'.
>
> But, I think you ought to look at method 'b' below ...
>
> $x = 'xval'; $y = 'yval';
>
> $hash{$x}{$y}{'value'} = 'worked';
> $hash{$x}{$y}{'value2'} = 'not worked';
>
> # method a
>
> $hashfmt{'1'} = "abc, 123, $hash{$x}{$y}{'value'}, xyz, 345,
> $hash{$x}{$y}{'value2'}" ;
> @values = split(/, |,/ ,$hashfmt{'1'}) ; # note I switch alternatives
>
> print "a: $values[2], $values[5]\n";
>
> # method b
>
> @values = ('abc', 123, $hash{$x}{$y}{'value'}, 'xyz', 345,
> $hash{$x}{$y}{'value2'});
> print "b: $values[2], $values[5]";
>
> ... prints
>
> a: worked, not worked
> b: worked, not worked
>
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED]]On Behalf Of Ed
> > DeBus
> > Sent: Tuesday, December 04, 2001 2:27 AM
> > To: [EMAIL PROTECTED]
> > Subject: dynamic variable evaluation
> >
> >
> >
> > Given the following scenario, is there a clean way to perform variable
> > substitution....
> >
> > First, we have a 3 dimensional hash:
> >
> >     $hash{$x}{$y}{'value'} = "it worked" ;
> >     $hash{$x}{$y}{'value2'} = "didn't work" ;
> >
> >
> > And another "reference" hash (nothing to do with perl ref's, but
> > rather for
> > building an array dynamically)
> >
> >     $hashfmt{'1'} = "abc, 123, \$hash{\$x}{$y}{'value'}, xyz, 345,
> > \$hash{\$x}{\$y}{'value2'}" ;
> >
> >
> > Now build an array dynamically using format defined by the
> > "reference" hash.
> >
> >     @values = split(/,|, / ,$hashfmt{'1'}) ;
> >
> >     $count=1 ;
> >     foreach $item (@values) {
> >             ?????< do what here> if ($count % 3 == 0) ;
> >             print "Item = [$item]\n" :
> >             $count++ ;
> >     }
> >
> >
> > The problem I have is getting every 3ed item in the array (array position
> > 2,5, etc.) to evaluate and return a value of "it worked" or "didn't work".
> > The reference hash format(s) are configured when the script starts and
> > remain static from that point forward.  It's probably obvious, but keys to
> > the 1st and 2nd dimensions of the "reference" hash are passed as variables
> > from a calling subroutine.  Probably important to also mention that the
> > values for the 1st and 2nd keys of the 3 dimensional hash can/do change
> > frequently.
> >
> > Any help/idea's appreciated....
> >
> > Ed
> >
> > _______________________________________________
> > Perl-Win32-Users mailing list
> > [EMAIL PROTECTED]
> > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> >
> >
>
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
>

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to