Professional Web Applications Themes

Creating hash with multiple keys for an array - PERL Beginners

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...

  1. #1

    Default Creating hash with multiple keys for an array

    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



    Edward Guest

  2. #2

    Default Re: Creating hash with multiple keys for an array

    Edward Wijaya wrote: 

    Yes.

    my %HoA = ( key1 => [ array1 ] );
    $HoA{key2} = $HoA{key1};
     

    Is that an array with 7 elements?
    What do you mean by the scalar variable $Key1 that points to 2
    numbers?
     

    Now I for one am lost.
     

    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
    Gunnar Guest

  3. #3

    Default Re: Creating hash with multiple keys for an array

    Thanks so much for your reply Gunnar,
     
    >
    > Is that an array with 7 elements?[/ref]

    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.

    I am sorry for my unclear statement previously.

     

    Am I doing the right thing by using hash with multiple keys
    for sorting purpose?

    Regards
    Edward WIJAYA
    SINGAPORE
    Edward Guest

  4. #4

    Default Re: Creating hash with multiple keys for an array

    Edward Wijaya wrote: 
    >>
    >> Is that an array with 7 elements?[/ref]
    >
    > 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.[/ref]

    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','AGCCAGGAG' ],
    '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
    Gunnar Guest

  5. #5

    Default Re: Creating hash with multiple keys for an array

    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


    Edward Guest

Similar Threads

  1. multidimentional hash that has both two and three keys
    By Trina Espinoza in forum PERL Beginners
    Replies: 1
    Last Post: January 30th, 12:06 PM
  2. hash of hash of array slices
    By Paul Kraus in forum PERL Beginners
    Replies: 6
    Last Post: January 26th, 05:55 PM
  3. choose from duplicate hash keys
    By JohnWalter in forum PERL Miscellaneous
    Replies: 17
    Last Post: September 27th, 10:38 AM
  4. Replies: 0
    Last Post: July 16th, 12:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139