dynamic name of a variable in a class

Discussion in 'Perl Misc' started by Brownie, Dec 4, 2008.

  1. Brownie

    Brownie Guest

    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
     
    Brownie, Dec 4, 2008
    #1
    1. Advertising

  2. Brownie <> 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
     
    Peter Makholm, Dec 4, 2008
    #2
    1. Advertising

  3. Brownie <> 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/"
     
    Tad J McClellan, Dec 4, 2008
    #3
  4. Peter Makholm <> wrote:
    > Brownie <> 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/"
     
    Tad J McClellan, Dec 4, 2008
    #4
  5. Brownie

    Guest

    On Thu, 4 Dec 2008 07:11:51 -0600, Tad J McClellan <> wrote:

    >Brownie <> 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"}...
     
    , Dec 4, 2008
    #5
  6. Brownie

    Guest

    Brownie <> 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.
     
    , Dec 4, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Chamness
    Replies:
    2
    Views:
    2,415
    Joe Smith
    Apr 22, 2007
  2. mfglinux
    Replies:
    11
    Views:
    726
    Roberto Bonvallet
    Sep 12, 2007
  3. Peter Buckley
    Replies:
    5
    Views:
    206
    matt neuburg
    Feb 27, 2009
  4. Klaus Johannes Rusch

    Reusing class name as variable name

    Klaus Johannes Rusch, Apr 19, 2004, in forum: Javascript
    Replies:
    7
    Views:
    153
    Michael Winter
    Apr 20, 2004
  5. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
Loading...

Share This Page