Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:34:58+00:00 2026-06-13T22:34:58+00:00

I have written the following code in Perl. The code is reading a pdb

  • 0

I have written the following code in Perl. The code is reading a pdb file and getting some values. Ignore the top part of the code,where everything is working perfect.
Problem is in the sub-routine part, where I try to store arrays in the hash3 with model as key another key position
the array values can be accessed inside the if condition using this :

$hash3{$model}{$coordinates}[1].

but when I go out of all foreach loop and try to access the elements I only get one value.
Please look at the end foreach loop and tell me is it the wrong way to access the hash values.

The pdb file I am using can be downloaded from this link http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1NZS

#!/usr/bin/perl
open(IN,$ARGV[0]);

my @phosphosites;
my $model=1;
my %hash3;
while(<IN>)
  {
    #findmod(@line);
    #finddist;
    #findfreq;
    if((/^MODRES/) && (/PHOSPHO/))
      {
        @line=split;
        push(@phosphosites, $line[2]);
        #print "$line[4]";
      }
    foreach $elements (@phosphosites){
      if(/^HETATM\s+\d+\s+CA\s+$i/)
        {
          @line1=split;
          #print "$line1[5]";
          #print "$line1[6] $line1[7] $line1[8]\n";
          push(@phosphositesnum, $line1[5]);
        }
    }
    $pos=$line1[5];
    #findspatial(\@line,\@line1);
  }

my @ori_data=removeDuplicates(@phosphositesnum);
sub removeDuplicates {
  my %seen = ();
  my @vals = ();

  foreach my $i (@_) {
    unless ($seen{$i}) {
      push @vals, $i;
      $seen{$i} = 1;
    }
  }

  return @vals;
}

$a=(@phosphosites);
print "$a\n";
print "@phosphosites\n";
print "@ori_data\n";

close(IN);

open(IN1,$ARGV[0]);
my (@data)=<IN1>;
spatial(\@ori_data);
sub spatial {

  my @spatial_array1=@{$_[0]};

  foreach $coordinates(@spatial_array1)
    {
      $model=1;
      {foreach $data1(@data){



        if($data1=~ m/^HETATM\s+\d+\s+CA\s+[A-Z]*\s+[A-Z]*\s+$coordinates/)
          {
            @cordivals=split(/\s+/,$data1);
            push @{ $sphash{$model} },[$cordivals[6], $cordivals[7], $cordivals[8]];
            $hash3{$model}{$coordinates}= \@cordivals;
            #print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";
            #print "$model $sphash{$model}[$i][0] $sphash{$model}[$i][1] $sphash{$model}[$i][2]\n";

          }

        elsif($data1=~ m/^ENDMDL/)
          {
            $model++;
          }
        #print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";


      }
     }
    }

    #foreach $z1 (sort keys %hash3)
    #  {
    #    foreach $z2(@spatial_array1){
    #      print "$z1 $z2";
    #      print "$hash3{$z1}{$z2}[6]\n";
    #      print "$z2\n";
    #    }
    #  }
}

After using the Data::Dumper option it is giving me this kind of output

$VAR1 = {
      '11' => {
                '334' => [
                           'HETATM',
                           '115',
                           'CA',
                           'SEP',
                           'A',
                           '343',
                           '-0.201',
                           '-2.884',
                           '1.022',
                           '1.00',
                           '99.99',
                           'C'
                         ],

                '342' => $VAR1->{'11'}{'334'},

                '338' => $VAR1->{'11'}{'334'},

                '335' => $VAR1->{'11'}{'334'},
                '340' => $VAR1->{'11'}{'334'},

                '343' => $VAR1->{'11'}{'334'},

                '336' => $VAR1->{'11'}{'334'}
              },

      '7' => {

               '334' => $VAR1->{'11'}{'334'},

               '342' => $VAR1->{'11'}{'334'},

               '338' => $VAR1->{'11'}{'334'},

               '335' => $VAR1->{'11'}{'334'},

               '340' => $VAR1->{'11'}{'334'},

               '343' => $VAR1->{'11'}{'334'},

               '336' => $VAR1->{'11'}{'334'}

             },

      '2' => {

               '334' => $VAR1->{'11'}{'334'},

               '342' => $VAR1->{'11'}{'334'},
               ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:34:59+00:00Added an answer on June 13, 2026 at 10:34 pm

    Change:

    @cordivals=split(/\s+/,$data1);
    

    to:

    my @cordivals=split(/\s+/,$data1);
    

    What seems to be happening is that all the hash elements contain references to the same array variable, because you’re not making the variable local to that iteration.

    In general, you should use my with all variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code in Perl. I want to iterate through a
I have written a following code to get just the file name without extension
I have written the following code in Perl. I have ActivePerl 5.14 for Windows
I have written the following code.. installFunction(){ perl Makefile.PL flag1 = $? make flag2
Today, I have written a perl script, as the following code: my @files =
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
i have written the following code class Program { static void Main(string[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.