Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Help Required

Status
Not open for further replies.

biobrain

MIS
Joined
Jun 21, 2007
Messages
90
Location
GB
Dear All,

I have written this code and it is working as follow,

1. Read the directory 2. Get all file name stored in @all_pdb_files array with .pdb extension
3. Print the information in and output.text file

opendir(DIR,"/home/Desktop/parser") or die "$!";
my @all_pdb_files = grep {/\.pdb$/} readdir DIR;
close DIR;

# Output file
open(OUTPUT,">output.txt");

foreach (@all_pdb_files){
print(OUTPUT "$_ \n");
}

But I need some more help to work on this script.

I want to retrive some particular information from each files. Here is Pseudo code for that I am not sure how to write the actual code.


opendir(DIR,"/home/Desktop/parser") or die "$!";
my @all_pdb_files = grep {/\.pdb$/} readdir DIR;
close DIR;

ForEach @all_pdb_files

1. Open and read each of these files

2. and Get/Match last word in the first line of each file

3. Store this last word from each file in a single file output.txt


Please help me in this regards
Regards
 
last word" is a somewhat ambiguous concept but this might work OK:

Code:
[url=http://perldoc.perl.org/functions/chdir.html][black][b]chdir[/b][/black][/url][red]([/red][red]'[/red][purple]/home/Desktop/parser[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR,[red]'[/red][purple].[/purple][red]'[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@all_pdb_files[/blue]  = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple][purple][b]\.[/b][/purple]pdb$[/purple][red]/[/red][red]}[/red] [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url] DIR[red];[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] DIR[red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red]OUT, [red]'[/red][purple]>output.txt[/purple][red]'[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][blue]@all_pdb_files[/blue][red])[/red] [red]{[/red]
   [olive][b]unless[/b][/olive] [red]([/red] [black][b]open[/b][/black][red]([/red][black][b]my[/b][/black] [blue]$FH[/blue], [blue]$file[/blue][red])[/red][red])[/red][red]{[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Unable to open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
      [olive][b]next[/b][/olive][red];[/red]
   [red]}[/red]
   [black][b]print[/b][/black] OUT [red]"[/red][purple][blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/readline.html][black][b]readline[/b][/black][/url][red]([/red][blue]$FH[/blue][red])[/red] =~ [red]/[/red][purple]^.*?([purple][b]\S[/b][/purple]+)$[/purple][red]/[/red][red])[/red][red];[/red]
   [black][b]close[/b][/black] [blue]$FH[/blue][red];[/red]
[red]}[/red]
[black][b]close[/b][/black][red]([/red]OUT[red])[/red][red];[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for your reply, This really helped me to learn a lot.

I have also tried this and it seems also working.

Code:
opendir(DIR,"/home/shafiq/Desktop/parser") or die "$!";
my @all_pdb_files  = grep {/\.pdb$/} readdir DIR;
close DIR;


open(OUTPUT,">output.txt");

foreach (@all_pdb_files){

open (SP, "$_");
while (<SP>){
   if ($_=~/(somthing)/ )
   {
   printf(OUTPUT " <li> $_ </li>");
   print (OUTPUT "<br>\n");
   }
}
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top