Search code examples
perlmultidimensional-arraymoose

How could I update a specific element of a 2D array with Moose in Perl 5?


Here is a class I created using Moose and I would like to do the following : update every element of the 2D Array Stack_dG IF and ONLY IF the object is built with a Temperature other than the default one of 37 through the subroutine '_scale_energy_parameters'.

Now, I know I can access a value in Stack_dG with $self->Stack_dG->[$i][$j]. But how could I update this value (need to set another one) ?

I have shown a part of code, but I have plenty of pairs of *_dG and *_dH attributes that I would like to update every *_dG through the '_scale_energy_parameters' method. Is there any way to do it without creating a different handle for each ?

Thanks in advance ! P.S. It's my first project with Moose and I'm open to any advice you could fill me with ! LP

#############################
# Energy.pm
#############################
{
    package Energy; 
    use Moose;
    use Moose::Util::TypeConstraints;
    use Moose::Meta::Attribute::Native::Trait::Array;

    # Constant to convert °C<=>°K
    has k0 => (is => 'ro', isa => 'Num', default => 273.15);
    # PUBLIC double Tmeasure = 37+K0;  /* temperature of param measurements */
    has Temperature => (is => 'ro', isa => 'Int', default => 37, trigger => \&_scale_enery_parameters);

    sub _scale_enery_parameters
    {
        my $self = shift;
        for(my $i=0;$i<8;$i++)
        {
            for(my$j=0;$j<8;$j++)
            {
                $self->update_Stack(8*$i+$j, ($self->Stack_dH->[$i][$j] - ($self->Stack_dH->[$i][$j] - $self->Stack_dG->[$i][$j]) * ($self->Temperature()+$self->k0()) ));
            }
        }
    }

    # Stack_d*->[NBPAIRS+1][NBPAIRS+1]
    has Stack_dG => (
        is => 'rw',
        isa => 'ArrayRef[ArrayRef[Int]]',
        handles => { update_Stack => 'set'},
        default => sub {
            [
                [  1e7,   1e7,   1e7,   1e7,   1e7,   1e7,   1e7,   1e7 ],
                [  1e7,  -240,  -330,  -210,  -140,  -210,  -210,  -140 ],
                [  1e7,  -330,  -340,  -250,  -150,  -220,  -240,  -150 ],
                [  1e7,  -210,  -250,   130,   -50,  -140,  -130,   130 ],
                [  1e7,  -140,  -150,   -50,    30,   -60,  -100,    30 ],
                [  1e7,  -210,  -220,  -140,   -60,  -110,   -90,   -60 ],
                [  1e7,  -210,  -240,  -130,  -100,   -90,  -130,   -90 ],
                [  1e7,  -140,  -150,   130,    30,   -60,   -90,   130 ]
            ]
        }
    );

    has Stack_dH => (
        is => 'rw',
        isa => 'ArrayRef[ArrayRef[Int]]',
        default => sub {
            [
                [ 1e7,   1e7,   1e7,   1e7,   1e7,   1e7,   1e7,   1e7],
                [ 1e7, -1060, -1340, -1210,  -560, -1050, -1040,  -560],
                [ 1e7, -1340, -1490, -1260,  -830, -1140, -1240,  -830],
                [ 1e7, -1210, -1260, -1460, -1350,  -880, -1280,  -880],
                [ 1e7,  -560,  -830, -1350,  -930,  -320,  -700,  -320],
                [ 1e7, -1050, -1140,  -880,  -320,  -940,  -680,  -320],
                [ 1e7, -1040, -1240, -1280,  -700,  -680,  -770,  -680],
                [ 1e7,  -560,  -830,  -880,  -320,  -320,  -680,  -320]
            ]
        }
    );
__PACKAGE__->meta->make_immutable;
    no Moose;   # turn off Moose-specific scaffolding
    1;
}

Solution

  • You should be able to simply assign to it:

    $self->Stack_dG->[$i][$j] = 42;
    

    Or am I misunderstanding the question?