Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to make four hash in one file, for one hash it works well, I want to make a sub create_hash, it can not give me any output.I have no idea how to make it. Please help! Thanks. In main part, it works: <BR<#Create hash


#Open the output file
my $outputfile="myhash";
unless( open(MYHASH, ">$outputfile")){
print "Can't open file \"$outputfile\" to write to!\n";
  exit;}


#Create hash keys and values array
my %myhash=();
#I use mydnahash' values to be myhash's values
@myhash{@reg_data}=@mydnahash{@reg_data};


#Create hash
while(($k, $v)=each %myhash){
  print MYHASH "$k=>$v, "};

I have to repeat it four times do the same thing, so I want to create a sub hash,how can I make this kind of sub? Thank you!

Replies are listed 'Best First'.
Re: create subroutine
by Popcorn Dave (Abbot) on May 04, 2002 at 21:37 UTC
    What about creating a 4 hashes with in your original hash or do you need 4 seperate hashes?

    You could do something along the lines of:

    $myhash = {my_key=> {sub_key=>sub_data} }

    where my key is 1-4 depending on which hash it is, and then your data is easily divided in the subhash.

    Without some kind of sample data it's sort of hard to get a feel for what you want, but I hope that helps.

Re: create subroutine
by Anonymous Monk on May 04, 2002 at 22:36 UTC
    Thank you so much popcorn Dave, My problem has one main hash, which is like {1=>A, 2=>C, 3=>G, 4=>T,....}, other four hash are all take part of the main hash keys and the correspond main hash values. Every hash keys are a long list array.