Connecting Tech Pros Worldwide Help | Site Map

Get Values for the id's

Member
 
Join Date: Sep 2006
Posts: 79
#1: Feb 11 '09
Hi,

I have an array like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. @arr="(mouse 18340633
  3.  
  4. sperm 18340633
  5.  
  6. proteome 18340633
  7.  
  8. strip 18340633
  9.  
  10. Role 18184912
  11.  
  12. insulator 18184912 )";
  13.  
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $join=join("#",@arr);
  3.  
  4. $join=~/(.*)\#(.*);
  5.  
  6. push(@id,$2);
  7. push(@words,$1);
  8.  
  9. #push(@temp,$join);
  10.  
  11. %seen;
  12.         for ( my $i = 0; $i <= $#id; )
  13.         {
  14.                 splice @id, --$i, 1
  15.                 if $seen{$id[$i++]}++;
  16.         }
  17.  
  18.   for($j=0;$j<scalar(@id);$j++)
  19.   {
  20.     print "<br> $words[$j] <br>";
  21.   }
  22.  
  23.  
I want the ouput like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 18340633: mouse, sperm ,proteome,strip  
  3.  
  4. 18184912: Role, insulator 
  5.  
How can i get the words?

Regards
Archana
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Feb 11 '09

re: Get Values for the id's


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

Expand|Select|Wrap|Line Numbers
  1. my %hash =  (
  2.    '18340633' => [qw(mouse sperm proteome strip)],
  3.    '18184912' => [qw(Role insulator)],
  4. );
  5.  
  6. foreach my $key (keys %hash) {
  7.    print "$key ", join (",", @{$hash{$key}}), "\n";
  8. }
The code you posted is full of errors and will not even compile.
Member
 
Join Date: Sep 2006
Posts: 79
#3: Feb 11 '09

re: Get Values for the id's


Quote:

Originally Posted by KevinADC View Post

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

Expand|Select|Wrap|Line Numbers
  1. my %hash =  (
  2.    '18340633' => [qw(mouse sperm proteome strip)],
  3.    '18184912' => [qw(Role insulator)],
  4. );
  5.  
  6. foreach my $key (keys %hash) {
  7.    print "$key ", join (",", @{$hash{$key}}), "\n";
  8. }
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?
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Feb 11 '09

re: Get Values for the id's


Quote:

Originally Posted by Archanak View Post

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