Get Values for the id's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Archanak
    New Member
    • Sep 2006
    • 79

    Get Values for the id's

    Hi,

    I have an array like this:

    Code:
    @arr="(mouse 18340633
     
    sperm 18340633
    
    proteome 18340633
    
    strip 18340633
    
    Role 18184912
    
    insulator 18184912 )";
    I want to retrieve the words that corresponds to that id.

    I tried removing duplicates and getting the words but it didn't work!!!

    Here is the code:

    Code:
    $join=join("#",@arr);
    
    $join=~/(.*)\#(.*);
    
    push(@id,$2);
    push(@words,$1);
    
    #push(@temp,$join);
    
    %seen;
            for ( my $i = 0; $i <= $#id; )
            {
                    splice @id, --$i, 1
                    if $seen{$id[$i++]}++;
            }
    
      for($j=0;$j<scalar(@id);$j++)
      {
        print "<br> $words[$j] <br>";
      }
    I want the ouput like this:

    Code:
    18340633: mouse, sperm ,proteome,strip  
     
    18184912: Role, insulator
    How can i get the words?

    Regards
    Archana
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    What you really want is a hash (of arrays), not an array.

    Code:
    my %hash =  (
       '18340633' => [qw(mouse sperm proteome strip)],
       '18184912' => [qw(Role insulator)],
    );
    
    foreach my $key (keys %hash) {
       print "$key ", join (",", @{$hash{$key}}), "\n";
    }
    The code you posted is full of errors and will not even compile.

    Comment

    • Archanak
      New Member
      • Sep 2006
      • 79

      #3
      Originally posted by KevinADC
      What you really want is a hash (of arrays), not an array.

      Code:
      my %hash =  (
         '18340633' => [qw(mouse sperm proteome strip)],
         '18184912' => [qw(Role insulator)],
      );
      
      foreach my $key (keys %hash) {
         print "$key ", join (",", @{$hash{$key}}), "\n";
      }
      The code you posted is full of errors and will not even compile.
      Hi,

      oh ok!!!

      sorry for that!!!

      How to create an hash for that?

      This is just an example of the values!!

      How can i create hash dynamically?

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by Archanak
        Hi,

        oh ok!!!

        sorry for that!!!

        How to create an hash for that?

        This is just an example of the values!!

        How can i create hash dynamically?
        Depends on the input data. Post an example of the data you are using to create the hash.

        Comment

        Working...