Jump to content

Reading Text File, Only certain columns...


vidyashankara

Recommended Posts

Here's a Text File.
[code]
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  HOH !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
HETATM @#!@#  !@#!@#   !@#!@#  AC9 !@#!@# !@#!@# !@#!@#
[/code]

@#!@# 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?
Link to comment
Share on other sites

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:

[code]<?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>';

?>[/code]

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

[code]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] => !@#!@#
        )

)[/code]

What is easy enough to work with I believe.
Link to comment
Share on other sites

[!--quoteo(post=381936:date=Jun 9 2006, 12:47 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 9 2006, 12:47 PM) [snapback]381936[/snapback][/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:

[code]<?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>';

?>[/code]

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

[code]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] => !@#!@#
        )

)[/code]

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!
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.