vidyashankara Posted June 14, 2006 Share Posted June 14, 2006 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.45ATOM 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?ThanksVidyashankra. Quote Link to comment https://forums.phpfreaks.com/topic/12031-reading-columns-from-a-text-file/ Share on other sites More sharing options...
dkphp2 Posted June 14, 2006 Share Posted June 14, 2006 That would be even easier, use substr():substr($str, 21, 1); // this gives you the 21st character for each line. Quote Link to comment https://forums.phpfreaks.com/topic/12031-reading-columns-from-a-text-file/#findComment-45753 Share on other sites More sharing options...
vidyashankara Posted June 15, 2006 Author Share Posted June 15, 2006 [!--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! Quote Link to comment https://forums.phpfreaks.com/topic/12031-reading-columns-from-a-text-file/#findComment-45762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.