dynamic name of a variable in a class

B

Brownie

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
 
P

Peter Makholm

Brownie said:
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
 
T

Tad J McClellan

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];
 
T

Tad J McClellan

Peter Makholm said:
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 ...

:)
 
S

sln

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"}...
 
X

xhoster

Brownie said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top