Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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, Im 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}; heres 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);

Comment on Parsing hash reference as argument for subroutine
Download Code
Re: Parsing hash reference as argument for subroutine
by choroba (Abbot) 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);

    Output:

    a 1.20000 0.20000 0.60000 b 0.60000 1.20000 0.80000 c 0.08000 0.06000 0.82000
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      thankyou!!
Re: Parsing hash reference as argument for subroutine
by Eily (Hermit) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069485]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-23 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (220 votes), past polls