Home > Archive > PERL Beginners > August 2004 > Creating hash with multiple keys for an array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Creating hash with multiple keys for an array
|
|
| Edward Wijaya 2004-08-07, 3:55 am |
| Hi,
Is there anyway in Perl to create
hash with multiple key for an array?
The purpose is as follows.
For example these lines:
AGCGGGGAG,AGCGGGGCG,AGCCGGGCG,AGCCAGGAG 15.188721875540
AGCGGAGCG,AGCCGAGGG,AGCGGAGGG 16.163408331891
\_____________________________________/ \_____________/
@Array1 $Key1
I want to store @Array1 under hash with $Key1 AND
scalar(@Array1) as $Key2.
Because later I need to sort the hash
based on $Key1 and $Key2.
Thanks so much for your time.
Regards,
Edward WIJAYA
SINGAPORE
| |
| Gunnar Hjalmarsson 2004-08-07, 3:55 am |
| Edward Wijaya wrote:
> Is there anyway in Perl to create hash with multiple key for an
> array?
Yes.
my %HoA = ( key1 => [ @array1 ] );
$HoA{key2} = $HoA{key1};
> The purpose is as follows.
>
> For example these lines:
>
> AGCGGGGAG,AGCGGGGCG,AGCCGGGCG,AGCCAGGAG 15.188721875540
> AGCGGAGCG,AGCCGAGGG,AGCGGAGGG 16.163408331891
> \_____________________________________/ \_____________/
> @Array1 $Key1
Is that an array with 7 elements?
What do you mean by the scalar variable $Key1 that points to 2
numbers?
> I want to store @Array1 under hash with $Key1 AND scalar(@Array1)
> as $Key2.
Now I for one am lost.
> Because later I need to sort the hash based on $Key1 and $Key2.
Does still not make sense to me.
Can you try to explain again what it is you want to know?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Edward Wijaya 2004-08-07, 3:55 am |
| Thanks so much for your reply Gunnar,
>
> Is that an array with 7 elements?
No. They are 2 arrays each with 4 elemeents and 3 elements.
For this I want to store them in hash of array.
> What do you mean by the scalar variable $Key1 that points to 2
> numbers?
What I mean by scalar of variable is : $Key2=scalar(@Array1)
i.e the number of elements of that array.
So $Key2 for line1 = 4,
and $Key2 for line2 = 3
I want to sort the hash based on this value as well as $Key1.
I am sorry for my unclear statement previously.
>
> Can you try to explain again what it is you want to know?
>
Am I doing the right thing by using hash with multiple keys
for sorting purpose?
Regards
Edward WIJAYA
SINGAPORE
| |
| Gunnar Hjalmarsson 2004-08-07, 8:55 am |
| Edward Wijaya wrote:
> Thanks so much for your reply Gunnar,
>
>
> No. They are 2 arrays each with 4 elemeents and 3 elements. For
> this I want to store them in hash of array.
>
>
> What I mean by scalar of variable is : $Key2=scalar(@Array1) i.e
> the number of elements of that array.
>
> So $Key2 for line1 = 4,
> and $Key2 for line2 = 3
>
> I want to sort the hash based on this value as well as $Key1.
Okay. If I understand you correctly, you don't need any additional
keys to be able to sort by number of elements in the arrays, since
that info is still conveniently available.
my %HoA = (
'15.188721875540' =>
[ 'AGCGGGGAG','AGCGGGGCG','AGCCGGGCG','AGC
CAGGAG' ],
'16.163408331891' =>
[ 'AGCGGAGCG','AGCCGAGGG','AGCGGAGGG' ],
);
print "Sorted by keys\n";
for ( sort { $a <=> $b } keys %HoA ) {
print "$_: @{ $HoA{$_} }\n";
}
print "\n";
print "Sorted by number of elements\n";
for ( sort { @{ $HoA{$a} } <=> @{ $HoA{$b} } } keys %HoA ) {
print "$_: @{ $HoA{$_} }\n";
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Edward Wijaya 2004-08-07, 3:55 pm |
| Thanks so much for your reply Gunnar.
However there is a bit complication.
I just realize that hash table can only
return the values of "unique" key.
Please try to execute the code below along
with the attached file, and the target answer below for clarity,
(the current code return the deviated answer).
____BEGIN___________
do 'ic.pl';
my %HoA = (
'A' =>
[ 'AGCGGGGAG', 'AGCGGGGCG', 'AGCCGGGCG', 'AGCCAGGAG', ],
'B' =>
[ 'CGTGCCTCC', 'CGTCCCGCC', 'CGTGCCTCC', 'CGTCCCTCC',],
'C' =>
[ 'GCAGCTGGG', 'GCAGCTGGA', 'GGAGCTGGG', 'GGAGCTGAA',],
'D' =>
[ 'GAAGCTGAG', 'GGAGCTGGG', 'GGAGCTGAA'],
'E' =>
[ 'GCCCCGCAG', 'GCCCCCCAC', 'GTCCCCCAG'],
'F' =>
[ 'GGAAGCTGA', 'GGGAGCTGG', 'GGGAGCTGA'],
);
foreach my $mykey ( sort keys %HoA ) {
$ic_value = compute_ic(@{$HoA{$mykey}});
# Not sure how to incorporate the $ic_value as another
# hash field for later sorting purpose.
#print "$mykey: ", join( ",", @{$HoA{$mykey}} ), " ", $ic_value,"\n";
}
print "Sorted by Values\n";
for ( sort { $a <=> $b } keys %HoA ) {
print "$_: @{ $HoA{$_} }", " ",compute_ic(@{ $HoA{$_} }),"\n";
}
print "\n";
print "Sorted by number of elements\n";
for ( sort { @{ $HoA{$a} } <=> @{ $HoA{$b} } } keys %HoA ) {
print "$_: @{ $HoA{$_} }", " ",compute_ic(@{ $HoA{$_} }),"\n";
}
____END___________
___TARGET ANSWER_____
Sorted by Values
A: AGCGGGGAG AGCGGGGCG AGCCGGGCG AGCCAGGAG 15.1887218755409
C: GCAGCTGGG GCAGCTGGA GGAGCTGGG GGAGCTGAA 15.1887218755409
D: GAAGCTGAG GGAGCTGGG GGAGCTGAA 15.2451124978365
E: GCCCCGCAG GCCCCCCAC GTCCCCCAG 15.2451124978365
F: GGAAGCTGA GGGAGCTGG GGGAGCTGA 16.163408331891
B: CGTGCCTCC CGTCCCGCC CGTGCCTCC CGTCCCTCC 16.1887218755409
Sorted by number of elements
D: GAAGCTGAG GGAGCTGGG GGAGCTGAA 15.2451124978365
E: GCCCCGCAG GCCCCCCAC GTCCCCCAG 15.2451124978365
F: GGAAGCTGA GGGAGCTGG GGGAGCTGA 16.163408331891
A: AGCGGGGAG AGCGGGGCG AGCCGGGCG AGCCAGGAG 15.1887218755409
C: GCAGCTGGG GCAGCTGGA GGAGCTGGG GGAGCTGAA 15.1887218755409
B: CGTGCCTCC CGTCCCGCC CGTGCCTCC CGTCCCTCC 16.1887218755409
___________________
I honestly don't want to sound like a guy who treats
this forum as a help desk. But I really don't
have a clue how to proceed.
Thanks so much again for your time.
Regards,
Edward WIJAYA
SINGAPORE
| |
| Gunnar Hjalmarsson 2004-08-07, 8:55 pm |
| Edward Wijaya wrote:
> I just realize that hash table can only return the values of
> "unique" key.
And the compute_ic() function computes numbers that are not unique,
so they can't be hash keys, I see. You have a new problem.
We don't know much about the bigger picture here. Would possibly an
array of hashes be a suitable data structure?
use strict;
use warnings;
use Data::Dumper;
do './ic.pl';
my @AoH = (
{ values => ['AGCGGGGAG','AGCGGGGCG','AGCCGGGCG','AG
CCAGGAG'] },
{ values => ['AGCGGAGCG','AGCCGAGGG','AGCGGAGGG'] },
);
for ( 0..$#AoH ) {
$AoH[$_]->{ic} = compute_ic( @{ $AoH[$_]->{values} } );
}
print Dumper @AoH;
__END__
$VAR1 = {
'values' => [
'AGCGGGGAG',
'AGCGGGGCG',
'AGCCGGGCG',
'AGCCAGGAG'
],
'ic' => '15.1887218755409'
};
$VAR2 = {
'values' => [
'AGCGGAGCG',
'AGCCGAGGG',
'AGCGGAGGG'
],
'ic' => '16.163408331891'
};
> my %HoA = (
> 'A' =>
> [ 'AGCGGGGAG', 'AGCGGGGCG', 'AGCCGGGCG', 'AGCCAGGAG', ],
> 'B' =>
> [ 'CGTGCCTCC', 'CGTCCCGCC', 'CGTGCCTCC', 'CGTCCCTCC',],
> ...
> );
>
> print "Sorted by Values\n";
> for ( sort { $a <=> $b } keys %HoA ) {
That tries to do a numerical sort on keys that are no longer
numerical. If you had had warnings enabled (which you should...), Perl
would have told you so.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Edward Wijaya 2004-08-08, 3:55 am |
|
>
> my @AoH = (
> { values => ['AGCGGGGAG','AGCGGGGCG','AGCCGGGCG','AG
CCAGGAG'] },
> { values => ['AGCGGAGCG','AGCCGAGGG','AGCGGAGGG'] },
> );
>
> for ( 0..$#AoH ) {
> $AoH[$_]->{ic} = compute_ic( @{ $AoH[$_]->{values} } );
> }
>
> print Dumper @AoH;
>
Thanks Gunnar,
I managed to construct the Array of Hashes (@AoH) - Glad I did that!
Now, I don't have the clue of how to sort these hashes,
according to IC value and No of elements.
Currently this snippet printed out unsorted hash..
____BEGIN_______
for my $i(0..$#AoH) {
for my $on_ic (keys %{$AoH[$i]}) { #LINE 2
print "@{ $AoH[$i]->{values} }", " ","$AoH[$i]->{ic}\n";
}
}
____END____________
I also tried to ammend LINE 2 to sort according to IC
with this:
for my $on_ic (sort {$AoH[$i]->{ic}{$a}<=>$AoH[$i]->{ic}{$b}} keys
%{$AoH[$i]})
Doesn't work as well.
Badly hope to hear from you again.
Regards,
Edward WIJAYA
| |
| Gunnar Hjalmarsson 2004-08-08, 8:55 am |
| Edward Wijaya wrote:
>
>
> Thanks Gunnar,
> I managed to construct the Array of Hashes (@AoH) - Glad I did
> that! Now, I don't have the clue of how to sort these hashes,
> according to IC value and No of elements.
What have you done to find out?
perldoc -f sort
perldoc -q "sort an array"
I think you also need to read up on data structures:
perldoc perldsc
This is one suggestion:
print "Sorted by ic value\n";
for my $hashref ( sort { $a->{ic} <=> $b->{ic} } @AoH ) {
print "$hashref->{ic}: @{ $hashref->{values} }\n";
}
print "\n";
print "Sorted by number of elements\n";
for my $hashref ( sort {
@{ $a->{values} } <=> @{ $b->{values} }
} @AoH ) {
print "$hashref->{ic}: @{ $hashref->{values} }\n";
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Edward Wijaya 2004-08-08, 3:55 pm |
|
>
> What have you done to find out?
>
> perldoc -f sort
> perldoc -q "sort an array"
>
> I think you also need to read up on data structures:
>
> perldoc perldsc
>
Gunnar,
It works!
I can't express enough my gratitude for your
help and patience.
I apologize for having trouble you this far.
Actually I did some reading on array sorting.
However, I admit I didn't do enough reading especially
on the topic of "hash reference".
Gratefuly yours,
Edward WIJAYA
SINGAPORE
|
|
|
|
|