Jump to content

Reading Columns from a Text file.


vidyashankara

Recommended Posts

Lets say i have a text file. It contains

[code]
ATOM 1009 N L U A 163 37.984 -12.158 -19.748 1.00 78.45
ATOM 2700 CG ASP B  164 82.383 0.302 48.606 1.00110.37    
ATOM 2701 OD1 ASP B  168 82.019 0.628 49.756 1.00110.37    
[/code]

I want the script to count the Number of A or B in the 5th column.

Currently i exploded the file into lines and i called it with a for function to read the 5th column. The code is

[code]
$array = explode("\n",atom.txt);
for ($i=0; $i<count($array); $i++) {
   preg_match_all("/([^ \r\n]+)(?:[ ]+)*/", $array[$i], $n);
   $atom[$i] = $n[1];
   $atoms[$i] = $n[1][3];    
}

$atoms = array_unique($atoms);
sort($atoms);
[/code]

This does the job. But if you look carefully at the text file, it contains lines like

[code]
ATOM   1009  N   L U A 163      37.984 -12.158 -19.748  1.00 78.45           N  
[/code]

Where A is not the 5th column but the 6th, There is a space between L and U. In this case, It select U instead of A and reports it, which is wrong.

Can i make the script read from the 21st column of the text file? (each letter is in one column)

How do i do that?

Thanks
Vidyashankra.
Link to comment
Share on other sites

[!--quoteo(post=384027:date=Jun 14 2006, 06:47 PM:name=dkphp2)--][div class=\'quotetop\']QUOTE(dkphp2 @ Jun 14 2006, 06:47 PM) [snapback]384027[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That would be even easier, use substr():

substr($str, 21, 1); // this gives you the 21st character for each line.
[/quote]


Thanks a lot! that did the job. so simple!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.