By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Get Values for the id's

P: 79
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
Feb 11 '09 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Feb 11 '09 #2

P: 79
@KevinADC
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?
Feb 11 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
@Archanak
Depends on the input data. Post an example of the data you are using to create the hash.
Feb 11 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.