Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Parsing hash reference as argument for subroutine

by madM (Beadle)
on Jan 06, 2014 at 11:27 UTC ( #1069485=perlquestion: print w/replies, xml ) Need Help??
madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I´m trying to pass a matrix reference as argument to a printmatrix subroutine.. unfortunately without succes.. the matrix was stored in the variable $matrix={} as a hash of hashes $matrix->{$key}->{$value}; here´s what i have done.. any suggestions?
sub Matrix { foreach my $key (@aminos){ foreach my $key2 (@aminos){ $matrix->{$key}->{$key2}= $ratesfromfreq->{$key}->{$key2}* +$factor; } $matrix->{$key}->{$key} += (1-$factor); } sub printMatrix1 { my $matrix2; ($matrix2) = @_; foreach my $key (sort keys %$matrix2){ print $key." "x2; foreach my $key2 (sort keys %{$matrix2->{$key}}){ printf("%.5f ", $matrix2->{$key}->{$key2}); } print "\n"x2; } } my $P= Matrix(); printMatrix($P);

Replies are listed 'Best First'.
Re: Parsing hash reference as argument for subroutine
by choroba (Chancellor) on Jan 06, 2014 at 11:48 UTC
    I added use strict; use warnings; and fixed the reported problems. I also added some whitespace to make the code more readable and populated some variables to get any output:
    #!/usr/bin/perl use warnings; use strict; my @aminos = qw(a b c); my $matrix; my $ratesfromfreq = {a => {a => 2, b => 1, c => 3}, b => {a => 3, b => 2, c => 4}, c => {a => .4, b => .3, c => .1}}; my $factor = .2; sub Matrix { for my $key (@aminos) { for my $key2 (@aminos) { $matrix->{$key}{$key2} = $ratesfromfreq->{$key}{$key2} * $ +factor; } $matrix->{$key}{$key} += (1 - $factor); } return $matrix; } sub printMatrix1 { my $matrix2; ($matrix2) = @_; for my $key (sort keys %$matrix2) { print $key . " " x 2; for my $key2 (sort keys %{ $matrix2->{$key}} ) { printf '%.5f ', $matrix2->{$key}{$key2}; } print "\n" x 2; } } my $P= Matrix(); printMatrix1($P);


    a 1.20000 0.20000 0.60000 b 0.60000 1.20000 0.80000 c 0.08000 0.06000 0.82000
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Parsing hash reference as argument for subroutine
by Eily (Parson) on Jan 06, 2014 at 11:54 UTC

    You should use strict and warnings, it would have helped you avoid some trouble.

    $matrix is not declared, you should have a my $matrix = {}; at the beginning of the function. This function does not return $matrix. But then again, there are three { block opener but only two } block ends, this doesn't even compile.

    To see what's inside a nested data structure (like a hash of hashes), you can use Data::Dumper like this :

    use Data::Dumper; #At the top of the file ... print Dumper $P;

    And at last, you probably want to read perldsc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069485]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-08-20 03:02 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (313 votes). Check out past polls.