0

I have the following subroutine OutputingReorderedVectors, which aims to output vectors following some pre-specified requirements. However, the code just output blank files.

I think the problem should come from this following code segment, which involves re-order the key from the second-level hash of chainRollupDoc

  my @rollupArray = sort keys %chainRollupDoc;
  my @reorderedSS = ();
  foreach my $i(0 .. $#rollupArray)
    {
        foreach my $cui (sort keys %{$chainRollupDoc->{$rollupArray[$i]}})
          {
            push @reorderedSS, $cui;
          }
    } 

The whole subroutine is in the following

#####################################
 sub OutputingReorderedVectors
#####################################
{
  my $centroids = shift;
  my $fileName = shift;
  my $chainRollupDoc = shift;   
  my @rollupArray = sort keys %chainRollupDoc;
  my @reorderedSS = ();
  foreach my $i(0 .. $#rollupArray)
  {
    foreach my $cui (sort keys %{$chainRollupDoc->{$rollupArray[$i]}})
    {
      push @reorderedSS, $cui;
    }
  } 

  my %attributes = ();
  foreach my $category (keys %$centroids)
  {
    foreach my $cui (keys %{$centroids->{$category}})
    {
      $features{$cui} = 1;
    }
  }
  my @fullSpace = sort keys %attributes;
  open(OUTPUT, "> $fileName");

  foreach my $i(0 .. $#reorderedSS)
  {
    printf OUTPUT "\t%s", $reorderedSS[$i];
  }
  print OUTPUT "\n";
  foreach my $i (0 .. $#fullSpace)
  {
    printf OUTPUT "%s", $fullSpace[$i];
    foreach  my $j (0 .. $#reorderedSS)
    {
      printf OUTPUT "\t%f", $centroids->{$reorderedSS[$j]}->{$fullSpace[$i]};
    }
    print OUTPUT "\n";
  }
  close OUTPUT;
}
3
  • 2
    Do you use use strict; and use warnings;? I've only been programming in Perl about 20 years and I know I don't spot all the problems that they do, so I essentially never code Perl without them. Commented Jan 1, 2012 at 20:35
  • 1
    Use array elements directly instead of using indexes. E.g. instead of for my $i (0 .. $#array) use for my $elem (@array) Commented Jan 1, 2012 at 20:41
  • 1
    use strict and use warnings until you know exactly why it is recommended to do so. Commented Jan 1, 2012 at 20:42

2 Answers 2

3

I don't see where %chainRollupDoc is declared and that makes me wonder if you're mixing up a hash ref with an actual hash. It looks like the function gets called with a hashref as the third argument since you say:

my $chainRollupDoc = shift;

but the next line then uses a hash called %chainRollupDoc. Maybe you meant this?

my @rollupArray = sort keys %$chainRollupDoc;

(note the dollar sign added to deref the hashref).

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

1 Comment

This is what I call OutputingReorderedVectors and pass chainRollupDoc OutputingReorderedVectors($centroid, $file1, $chainRollupDoc);
0

Assuming where you have:

$features{$cui} = 1;

You meant:

$attributes{$cui} = 1;

This is the simplified version of your code:

use warnings; use strict;
use List::MoreUtils qw'uniq';
use autodie;

sub OutputingReorderedVectors{
  my($centroids,$fileName,$chainRollupDoc) = @_;

  my @reorderedSS;
  for my $i( sort keys %$chainRollupDoc ){
    push @reorderedSS, sort keys %{$chainRollupDoc->{$i}}
  }
  # NOTE: @reorderedSS is NOT sorted, only parts of it are.

  my @fullSpace;
  for my $category (values %$centroids){
    push @fullSpace, keys %$category
  }
  @fullSpace = sort uniq @fullSpace;

  open my $output, '>', $fileName;

  print {$output} join( "\t", '', @reorderedSS ), "\n";

  for my $i (@fullSpace){
    print {$output} $i;
    for my $j (@reorderedSS){
      # could possibly be replaced with a simple print statement
      printf {$output} "\t%f", $centroids->{$j}->{$i};
    }
    print {$output} "\n";
  }
  close $output;
}

If you gave us an example of your data, and the expected output of that data, we could help you further.

I would like to point out that you are programming in Perl as if it were C.

Comments

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.