0

I wrote the following Perl function

  sub Outputing
  { 
     my $featureMatrix = shift;
     my $indexRow = shift;
     my $fileName = "/projectworkspace/input.dat";

     open(DATA, "> $fileName");
     printf DATA "%d", $#$indexRow;
     print DATA "\n";

     my $numDataPoints = $#{$featureMatrix{$indexRow->[1]}};
     printf DATA "%d", $numDataPoints;
     print DATA "\n";

     close DATA;    
    }

I calling Outputing as follows:

Outputing($matrix, $Rows);e

$matrix is a hash of array, whose structure is like this

   my $matrix 
     = { 200 => [ 0.023, 0.035, 0.026 ], 
          110 => [ 0.012, 0.020, 0,033], 
        }; 

Rows is an array storing the sorted key of matrix, it is obtained as follows

my @Rows = sort keys %matrix;

both matrix and Rows are used as parameters passed to Outputing.

The printed out $numDataPoints is -1, which is not correct? I do not know which might be the reason that causes this problem? If we use the above example, and assume $indexRow->[1]=110, then $numDataPoints should be 2. I am not sure whether the $#{$featureMatrix{$indexRow->[1]}}; is the correct way to get the size of this array.

1 Answer 1

3

Assuming that you've included all the relevant code, this:

my @indexRow = sort keys %featureMatrix;

should be this:

my @indexRow = sort keys %$featureMatrix;

and this:

     my $numDataPoints = $#{$featureMatrix{$indexRow->[1]}};

should be this:

     my $numDataPoints = $#{$featureMatrix->{$indexRow->[1]}};

That is, the problem is that in some places, you're using a hash named %featureMatrix, and in others, you're using a hashref named $featureMatrix that refers to an anonymous hash.

You should be using use warnings and use strict to prevent such mistakes: those would have prevented you from using %featureMatrix when you've only declared $featureMatrix. (Actually, use warnings might not help in this case — it could detect if you used %featureMatrix exactly once, but in your case, you use it a few times — but use strict would almost certainly have helped.)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but I still not very clear about this. First, my @indexRow = sort keys %featureMatrix; was used outside the function of "Outputing" rather than inside the "Outputing". The variable name I used may cause some confusion. I will edit my original post.
I just edited my original post. Does that affect your answer? In accordance with your answer, I should have written my @Rows = sort keys %$matrix;
@user785099: Are you familiar with the difference between my $featureMatrix = { ... } and my %featureMatrix = ( ... )? Your question says you're using the former, but various parts of your code would only work if you were using the latter.
can you elaborate more on the difference between my $featureMatrix = { ... } and my %featureMatrix = ( ... ).
@user785099: my %featureMatrix = ( ... ) creates a hash named %featureMatrix. Its elements can be referred to with the notation $featureMatrix{'...'}. my $featureMatrix = { ... } creates an anonymous hash, and a reference to that hash. The reference is named $featureMatrix. The hash can be referred to with the notation @$featureMatrix, and its elements can be referred to with the notation $featureMatrix->{'...'}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.