Jump to content


Photo

Reading Text File, Only certain columns...


  • Please log in to reply
2 replies to this topic

#1 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 09 June 2006 - 04:34 PM

Here's a Text File.
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#

@#!@# can be any number/letter or anything.

The script should read this file, come up with this output
The file has 2 unique HETATM's, they are HOH and AC9. Some text files might have just HOH or just AC9 or neither, They might have something else. Any way out?

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 09 June 2006 - 04:47 PM

I believe I coded something like that for you before. It is here:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95300\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=95300[/a]

A slight modification:

<?php

$str = 'HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#';

// If you want to retrieve the values from a file, use this:
// $str = file_get_contents('file.txt');

$array = explode("\n", $str);

for ($i=0; $i<count($array); $i++) {
   preg_match_all("/([^ \r\n]+)(?:[ ]+)*/", $array[$i], $m);
   $matches[$i] = $m[1];
   $fifth[$i] = $m[1][4];
}

echo '<pre>';
print_r($matches);
echo '</pre>';

?>

$matches is an array and should be something like this:

Array
(
    [0] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [1] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [2] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [3] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [4] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [5] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [6] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [7] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [8] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [9] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [10] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [11] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

)

What is easy enough to work with I believe.
~ D Kuang

#3 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 09 June 2006 - 05:06 PM

[!--quoteo(post=381936:date=Jun 9 2006, 12:47 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 12:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I believe I coded something like that for you before. It is here:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95300\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=95300[/a]

A slight modification:

<?php

$str = 'HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#';

// If you want to retrieve the values from a file, use this:
// $str = file_get_contents('file.txt');

$array = explode("\n", $str);

for ($i=0; $i<count($array); $i++) {
   preg_match_all("/([^ \r\n]+)(?:[ ]+)*/", $array[$i], $m);
   $matches[$i] = $m[1];
   $fifth[$i] = $m[1][4];
}

echo '<pre>';
print_r($matches);
echo '</pre>';

?>

$matches is an array and should be something like this:

Array
(
    [0] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [1] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [2] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [3] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [4] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [5] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [6] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => HOH
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [7] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [8] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [9] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [10] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

    [11] => Array
        (
            [0] => HETATM
            [1] => @#!@#
            [2] => !@#!@#
            [3] => !@#!@#
            [4] => AC9
            [5] => !@#!@#
            [6] => !@#!@#
            [7] => !@#!@#
        )

)

What is easy enough to work with I believe.
[/quote]


Yeah the 2 scripts are similar. I couldnt figure out how to modify it. The last one was just one letter. I figured a 3 letter word might be complex.




Hey, Thanks! I really appreciate your help! :) you rock!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users