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

use a list to extract out the data...

P: 3
Hi,
I'm new to Perl... I have some questions to extract the data out.
I have two files. One is gene list, and the other is the whole data(having gene lists and other values).
I'd like to extract the informations of the input lists.

I tried... but the RESULT file is empty!! I don't know what's wrong...
Here is my code.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2.  
  3. open(Data,"data.txt");
  4. @all = <Data>;
  5. close(Data);
  6.  
  7. open(List,"list.txt");
  8. @list = <List>;
  9. close(List);
  10.  
  11. open(ANS,">result.txt");
  12.  
  13. for($j=0;$j<$#list;$j++){
  14.  
  15.     chomp($list[$j]);
  16.  
  17.                }
  18.  
  19. for($i=0;$i<$#all;$i++){
  20.  
  21.     @b=split("\t",$all[$i]);
  22.  
  23.    for($j=0;$j<$#list;$j++){
  24.  
  25.     if($b[0] eq $list[$j]){
  26.  
  27.         print ANS "$all[$i]"; 
  28.  
  29.                }
  30.          }
  31. }
  32.  
  33. close(ANS);






Thanks!!!
May 21 '10 #1
Share this Question
Share on Google+
5 Replies


Expert
P: 70
Since you didn't show any input data, you must debug your own code. Follow the tips in the Perl Basic debugging checklist.

Specifically, you should check the success of each file open, then start adding print statements to your code.
May 22 '10 #2

P: 3
Hi,
here are my file contents(.jpg).
i've tried use the debugging checklist;
however, it didn't show anything.
Attached Images
File Type: jpg test.jpg (16.9 KB, 108 views)
May 22 '10 #3

Expert Mod 100+
P: 589
You're using the wrong data structure. Instead of 2 arrays, you should use 1 hash.

Load data.txt into a hash where the gene is the key and the value is the related data.

Then loop over list.txt and check if the gene is a key in the hash. If it is, then output the data.
May 22 '10 #4

P: 3
hi,
i'm really new to Perl. i can't really understand, but i have tried...
here is my code, please tell me where is the problem.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open(Data,"<data.txt");
  4. my %all= <Data>;
  5. my ($key, $value);
  6.  
  7. open(List,"<list.txt");
  8. my %list = <List>;
  9.  
  10. open(ANS,">result.txt");
  11.  
  12. while($all{$key} eq %list) {
  13.  
  14.         print ANS "%all"; 
  15.  
  16.                }
  17.  
  18. close(Data);
  19. close(List);
  20. close(ANS);
  21.  
May 22 '10 #5

Expert Mod 100+
P: 589
It doesn't appear that you understand the basics, so you probably wouldn't understand my descriptions on what you're doing wrong.

You need to start by getting a good beginner level Perl book.

Learning Perl, Fifth Edition
http://oreilly.com/catalog/9780596520113
May 22 '10 #6

Post your reply

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