Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with Hash

Status
Not open for further replies.

biobrain

MIS
Jun 21, 2007
90
GB
Dear All,

I have some problem in learning hash, please help me in this regards

I have lines

Myline anything123 A
Myline anything124 B
Myline anything768 C

I want to read them them line by line and to store A, B, C into Hash Key and to these assign an increasing value 1, 2, 3
 
Thanks for your message.

I have many lines

myline anything A anything
myline anything B anything
myline anything C anything
myline anything D anything
myline anything E anything
myline anything F anything
myline anything G anything
myline anything H anything
myline anything I anything

I am only interested in key A, B, C , D, E F etc from the line no other part of the line
 
Here is my original code

opendir(DIR,"/home/shafiq/Desktop/parser/max-cluster-prepration/") or die "$!";
my @all_pdb_files = grep {/\.pdb$/} readdir DIR;
close DIR;

foreach (@all_pdb_files){
open (SP, "$_");
my(%myhash,$counter);
while(<SP>){
if ( $_=~/SEQRES\s+\S+\s(\S)/){
#print $1;
$myhash{$1}="";
#print $myhash{B}
}



}


}


It is working and tested with print statement. Now I want that It should assigne a value 1 to first unique key, value 2 to 2nd key and so on
 
Code:
foreach (@all_pdb_files){    
    open (SP, "$_");
    my(%myhash,$counter);
    while(<SP>){
        # give us a live sample to advice you with a better looking and more readable regex
        if ( $_=~/SEQRES\s+\S+\s(\S)/){   
            # this counter gets set to the next number 
            # the first time will take the value of '1'
            # the second time the value of '2'
            
            $counter++;
            
            # I'm not sure which one of the two is what you want, as you didn't quite explain above. So its either the first line #1 or the second #2
            $myhash{$1} = $counter;     # 1
            $myhash{$counter} = $1;     # 2
        }
        ..............
        ..............
    }
    ................. ......
    ...........................
}

this line $myhash{$1} = $counter; will give you
Code:
$myhash{A} = 1
$myhash{C} = 2
$myhash{B} = 3
.....
this line $myhash{$counter} = $1; will give you
Code:
$myhash{1} = A
$myhash{2} = C
$myhash{3} = B
.....


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
SEQRES 1 A 366 GLY SER HIS MET LEU GLU MET SER GLN GLU ARG PRO THR
SEQRES 2 A 366 PHE TYR ARG GLN GLU LEU ASN LYS THR ILE TRP GLU VAL
SEQRES 1 C 406 GLY SER HIS MET LEU GLU MET LEU SER ASN SER GLN GLY
SEQRES 2 C 406 GLN SER PRO PRO VAL PRO PHE PRO ALA PRO ALA PRO PRO
SEQRES 3 C 406 PRO GLN PRO PRO THR PRO ALA LEU PRO HIS PRO PRO ALA
SEQRES 4 C 406 GLN PRO PRO PRO PRO PRO PRO GLN GLN PHE PRO GLN PHE
SEQRES 5 C 406 HIS VAL LYS SER GLY LEU GLN ILE LYS LYS ASN ALA ILE
SEQRES 6 C 406 ILE ASP ASP TYR LYS VAL THR SER GLN VAL LEU GLY LEU
SEQRES 7 C 406 GLY ILE ASN GLY LYS VAL LEU GLN ILE PHE ASN LYS ARG

Here is a sample of the original data. Well I want to read this data here A and C indicate the chains of protein.
 
Here in the above data there is A,B,C, E are keys.

Now I want to assign a value of 1 to A does not matter how many time it exists.

Then I want to assign a value of 2 to C in the above example but if there is B instead of C than 2 value for B and 3 for C and so on.

Some time there may not be A and Data start with B than the value of B will be 1
 
Code:
foreach (@all_pdb_files){    
   open (SP, "$_");
   my(%myhash,$counter);
   while(<SP>){
       if ( $_=~/SEQRES\s+\S+\s(\S)/){
           # place the chains in a array, unless that chain already checked

           push @chainsofprotein $1 unless $I_met_this_chain_before{$1};
           $I_met_this_chain_before{$1} = 1;
       }
        ..............
        ..............
   }
   # when you finish all lines of the file
   # then sort the array alphabetically and assign a number to it

   foreach my $chain ( sort @chainsofprotein ) {
       $counter++;
       $chain_hash{$chain} = $counter;
   }
    ................. ......
    ...........................
}

this will give you
Code:
$chain_hash{A} = 1
$chain_hash{B} = 2
$chain_hash{C} = 3
$chain_hash{D} = 4

or if there is no A chain ou will get

$chain_hash{B} = 1
$chain_hash{C} = 2
$chain_hash{D} = 3



``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top