Hello,
I know that my request is ugly, but I need tot do that.
I have a class :
**********************************
package Epoch;
use strict;
use PDL;
sub new {
my ($class) = @_;
my $self = {};
bless($self, $class);
$self->{SOL_L4_A} = Solution->new ();
$self->{SOL_L1_A} = Solution->new ();
return $self;
}
1;
***********************************
SOL_L4_A and SOL_L1_A are to objects of the same class, and the way to
define it is the same (just one parameter is changed in the main
algorithme)
so in the main i have :
[...]
dice_axis($epoch->{SOL_L1_A}->{XYZ},1,$k3-1) .= $solL1_temp[$k];
[...]
dice_axis($epoch->{SOL_L4_A}->{XYZ},1,$k3-1) .= $solL4_temp[$k];
I would like to do something like :
$SOLUTION = L1;
dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
$SOLUTION_temp[$k];
$SOLUTION = L4;
dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
$SOLUTION_temp[$k];
I my algorithm, it is not just two lines, ;)
Ty for your attention,
See You
|
|
0
|
|
|
|
Reply
|
brownie2002 (3)
|
12/4/2008 11:55:25 AM |
|
Brownie <brownie2002@gmail.com> writes:
> I would like to do something like :
> $SOLUTION = L1;
> dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
Hash keys are just strings, so beside barewords you can use any string
value as keys, including a double-quoted string:
$epoch->{"SOL_$SOLUTION_A"}->{...}
Or even real expressions:
$epoch->{join "_", "SOL", $SOLUTION, "A"}->{...}
Not that I would do that in you case
//Makholm
|
|
0
|
|
|
|
Reply
|
Peter
|
12/4/2008 12:21:33 PM
|
|
Brownie <brownie2002@gmail.com> wrote:
> I know that my request is ugly,
Do you mean that you have already read the FAQ answer for your question?
How can I use a variable as a variable name?
> but I need tot do that.
You only _think_ you need to do that.
Using a more appropriate data structure would avoid needing to use
symbolic references.
> I have a class :
> **********************************
> package Epoch;
>
> use strict;
> use PDL;
>
> sub new {
> my ($class) = @_;
> my $self = {};
> bless($self, $class);
>
> $self->{SOL_L4_A} = Solution->new ();
> $self->{SOL_L1_A} = Solution->new ();
>
> return $self;
> }
> 1;
> ***********************************
> SOL_L4_A and SOL_L1_A are to objects of the same class, and the way to
> define it is the same (just one parameter is changed in the main
> algorithme)
>
> so in the main i have :
> [...]
> dice_axis($epoch->{SOL_L1_A}->{XYZ},1,$k3-1) .= $solL1_temp[$k];
> [...]
> dice_axis($epoch->{SOL_L4_A}->{XYZ},1,$k3-1) .= $solL4_temp[$k];
You have not shown us the declaration for @solL1_temp...
If @solL1_temp is a package variable, then you _could_ use symbolic
references to do what you want (but there be dragons, as pointed out
in the FAQ answer).
If @solL1_temp is a lexical variable, then you _could_ use the
evil "eval EXPR" to do what you want (but there be even scarier
dragons)!
If you instead used a hash to contain your temp arrays, then you
would need neither.
Avoiding dragons is much much safer than slaying dragons. :-)
Instead of:
my @solL1_temp = ( 'one', 'two' );
my @solL4_temp = ( 'three', 'four' );
use a hash-of-arrays:
my %temp = (
solL1 => ['one', 'two' ],
solL4 => ['three', 'four' ],
);
> I would like to do something like :
> $SOLUTION = L1;
Strings in Perl need quotes:
$SOLUTION = 'L1';
> dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
^^^^^^^^^^^^^^^
> $SOLUTION_temp[$k];
Hash keys are auto-quoted for you only if they are bare words.
If they are not bare words, then you need to quote the hash keys yourself:
dice_axis($epoch->{"SOL_${SOLUTION}_A"}...
or, the somewhat uglier:
dice_axis($epoch->{'SOL_' . $SOLUTION . '_A'}...
Assuming you have adopted a more suitable data structure for your
temp arrays, you can then easily (and safely!) get what you need:
# untested
dice_axis($epoch->{"SOL_${SOLUTION}_A"}->{XYZ},1,$k3-1) .=
$temp{"sol$SOLUTION"}[$k];
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
|
|
0
|
|
|
|
Reply
|
Tad
|
12/4/2008 1:11:51 PM
|
|
Peter Makholm <peter@makholm.net> wrote:
> Brownie <brownie2002@gmail.com> writes:
>
>> I would like to do something like :
>> $SOLUTION = L1;
>> dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
>
> Hash keys are just strings, so beside barewords you can use any string
> value as keys, including a double-quoted string:
>
> $epoch->{"SOL_$SOLUTION_A"}->{...}
Global symbol "$SOLUTION_A" requires explicit package name at ...
:-)
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
|
|
0
|
|
|
|
Reply
|
Tad
|
12/4/2008 2:19:12 PM
|
|
On Thu, 4 Dec 2008 07:11:51 -0600, Tad J McClellan <tadmc@seesig.invalid> wrote:
>Brownie <brownie2002@gmail.com> wrote:
>
>
>> I know that my request is ugly,
>
>
>Do you mean that you have already read the FAQ answer for your question?
>
> How can I use a variable as a variable name?
>
>
>> but I need tot do that.
>
>
>You only _think_ you need to do that.
>
>Using a more appropriate data structure would avoid needing to use
>symbolic references.
>
[snip]
>> dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .= $sol
> ^^^^^^^^^^^^^^^
>> $SOLUTION_temp[$k];
>
>
>Hash keys are auto-quoted for you only if they are bare words.
>
>If they are not bare words, then you need to quote the hash keys yourself:
>
> dice_axis($epoch->{"SOL_${SOLUTION}_A"}...
>or, the somewhat uglier:
> dice_axis($epoch->{'SOL_' . $SOLUTION . '_A'}...
>
This works as well:
dice_axis($epoch->{"SOL_$SOLUTION\_A"}...
|
|
0
|
|
|
|
Reply
|
sln
|
12/4/2008 8:57:35 PM
|
|
Brownie <brownie2002@gmail.com> wrote:
> so in the main i have :
> [...]
> dice_axis($epoch->{SOL_L1_A}->{XYZ},1,$k3-1) .= $solL1_temp[$k];
So dice_axis is an l-value subroutine?
> $SOLUTION = L1;
L1 probably needs quotes.
> dice_axis($epoch->{SOL_$SOLUTION_A}->{XYZ},1,$k3-1) .=
> $sol$SOLUTION_temp[$k];
Solving your "variable in a class" problem is trivial.
$epoch->{SOL_${SOLUTION}_A}
The real problem is with @solL1_temp and kin. But you didn't show us the
part of the code that declares and defines those, so you you didn't show us
the part of the code that created the real problem.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
|
|
0
|
|
|
|
Reply
|
xhoster
|
12/4/2008 9:37:35 PM
|
|
|
5 Replies
61 Views
(page loaded in 0.094 seconds)
|