Ok, I guess I need to bring more context to the stage. The script itself is
a credit card batch parser, which guesses the card type by the first digit,
and proceeds by calculating totals, transaction counts, transaction fees,
and bunch of other relevant things. So ebverything in the script revolves
around hashes with the same keys (3,4,5,6). However at the end after all
calculations, because of a certain processing specifics, I need to produce a
visa + master grand total, if the user requests it (web for input). So I
dropped this snippet which simply creates an additional key vm, and adds
everything together. In this light I hope you understand that doing
this:$data{vm}{cardtot} = $data{4}{cardtot} + $data{5}{cardtot};
would complicate code even more than just plain adding them together.
Actually it is not that big of a deal since I am replacing 5 lines with 3,
not that much of a speed increase. However for aesthetical reasons I wanted
to know how to reference a variable, nevertheless in this case it is
completely justified. Granted the code will instantly break if some
doofus changes the names of the variables (the foreach list will be invalid
thereafter) but hey - who asked him to mess with the names?

As a matter of fact this probably was a bad example, I have another
application for this that is way more elaborate to write line by line.
Consider a cgi form that has a number of properties for a number of cards.
For example transaction fee, fee assessment, discount rate for all visa,
master, discover, amex. Say we name the Input types accordingly:

6_pct - discover discount rate
6_tfee - discover transaction fee
6_fa - discover fee assessment
5_pct - master card discount rate
....
you get the idea. Now I want to read them in the script:

$pct{6} = $form->param('6_pct');
$tfee{6} = $form->param('6_tfee');
$fa{6} = $form->param('6_fa');
$pct{5} = $form->param('5_pct');
....
Wow... what if I had 10 kinds of cards and 20 properties for each?

However if I could define:
%cards = (4,"Visa", 5,"MasterCard", 6,"Discover", 3,"AmEx");
@props = ("pct", "tfee", "fa");

and than do:
foreach $c (keys (%cards)) {
        foreach $p (@props) {
                $($p){$c} = $form->param('($c)."_".($p)');
        }
}

(or at least something along the lines, since this is obviously more
pseudo-langague than perl itself)

Disregard how "stupid" it is to use a variable as a variable name - I don't
see why it wouldn't be suitable above. Nevertheless I still don't know how
to do it, although Mr. Dominus says it is fairly easy.

So help me or shoot me :)


P.S. I personally think use strict complains just way too much, to a point
where you lose all the elegance that coding in perl calls for.

Peter

P.P.S. Sorry for the double message, did not reply to list. 

On Sun, Oct 10, 2004 at 09:34:13PM +0200, Jenda Krynicky wrote:
> From: Peter Rabbitson <[EMAIL PROTECTED]>
> > Hello list, 
> > I have the following code:
> > 
> > $cardtot{vm} = $cardtot{4} + $cardtot{5};
> > $trans{vm} = $trans{4} + $trans{5};
> > $dsc_amt{vm} = $dsc_amt{4} + $dsc_amt{5};
> > $trf_amt{vm} = $trf_amt{4} + $trf_amt{5};
> > $cardtot{vm} = $cardtot{4} + $cardtot{5};
> > $as_tot{vm} = $as_tot{4} + $as_tot{5};
> 
> You should use a hash like this:
> 
>       $data{vm}{cardtot} = $data{4}{cardtot} + $data{5}{cardtot};
>       $data{vm}{trans} = $data{4}{trans} + $data{5}{trans};
>       ...
>  
> > How can it be rewritten in a way similar to this?:
> > 
> > foreach $var ("cardtot", "trans", "dsc_amt", "trf_amt", "cardtot",
> > "as_tot") { $$var{vm} = $$var{4} + $$var{5}; }
> 
> Then it would be either:
> 
> foreach my $var ("cardtot", "trans", "dsc_amt", "trf_amt", 
>               "cardtot", "as_tot") {
>       $data{vm}{$var} = $data{4}{$var} + $data{5}{$var};
> }
> 
> or (if you were sure you want to do this for all keys in 
> $data{something} you could use just:
> 
> foreach my $var (keys %{$data{4}}) {
>       $data{vm}{$var} = $data{4}{$var} + $data{5}{$var};
> }
> 
>  
> > It does not work the way I wrote it, so I guess I am missing something
> > really really minor...
> 
> There are two things you are missing. 
> 1) What does $$var{key} mean? Is it the same as ${$var}{key} or 
> ${$var{key}}? That is should I first dereference and then look for a 
> key or the other way around? To tell the truth I don't remember 
> myself which one takes precedence so I would always use the {} to 
> make sure it means what I wanted.
> 
> 2) You are trying to use so called "soft references". While it is 
> possible (without "use strict") it's not a good idea. Please read:
> 
> Why it's stupid to `use a variable as a variable name' - by M-J. 
> Dominus 
> http://www.plover.com/~mjd/perl/varvarname.html
> 
> HTH, Jenda
> ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
> When it comes to wine, women and song, wizards are allowed 
> to get drunk and croon as much as they like.
>       -- Terry Pratchett in Sourcery
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to