Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: Retrieve data from text file...stuck...

  1. #11
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The solution james438 proffered will work, but is far from optimal. It is very memory-intensive if you have a lot of data, since it loads it all into memory at once. I strongly agree with the opinions expressed thus far about how much easier and more efficient this would be with a proper database. However, here is a somewhat better way of doing it using a buffer:
    Code:
    define('BUF_SIZ', 128);
    
    define('TYPE_COLUMN', 0);
    define('YEAR_COLUMN', 1);
    define('ID_COLUMN', 2);
    define('VALUE_COLUMN', 3);
    
    function select_from_file($get, $where, $val, $multiple = false) {
      $col %= 4;
      $r = $multiple ? array() : '';
      $f = open('yourfile.dat', 'r');
    
      for($current_line = '', $next_line = '', $pos; !feof($file);) {
        while(($pos = strpos($current_line, "\n")) === false)
          $next_line .= fread($f, BUF_SIZ);
        $next_line = str_replace("\r", '', $next_line);
        list($current_line, $next_line) = explode("\n", $next_line, 2);
        if($current_line) {
          $current_line = explode(' ', $current_line);
          if($current_line[$where] == $val)
            if($multiple)
              $r[] = $current_line[$get];
            else {
              $r = $current_line[$get];
              break 2;
            }
        }
      }
      fclose($f);
      return $r;
    }
    E.G.:
    Code:
    select_from_file(VALUE_COLUMN, ID_COLUMN, 2099) + select_from_file(VALUE_COLUMN, ID_COLUMN, 2641)
    is -225816.67. You can also use it for other columns; select_from_file(TYPE_COLUMN, VALUE_COLUMN, '-226930.95') will return '#IB', and select_from_file(VALUE_COLUMN, TYPE_COLUMN, '#IB', true) will return an array of values from rows that begin with #IB (if you don't pass the $multiple argument, only the first one will be selected).

    Untested.
    Last edited by Twey; 08-29-2007 at 10:12 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #12
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Take your time! I am very thankful for the help so far and don't feel pressure to deliver. I am the one who will be using this for various data.

  3. #13
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I updated the script one last time to allow for unlimited identifiers, but I don't plan on doing any more work with it. There is plenty there in the script for you to learn from.

    I really don't like the script much any more despite all I have learned from it in the process. This was good practice for me too. The code I wrote is too bulky. It is taxing on the server to say the least and in my experience these types of programs are just not as secure as databases. MySQL can do the same thing in one short line that is far easier to understand. The single MySQL line that Twey showed you above is all you really need.

    Lastly, and most importantly, this script does not teach good coding practices, so listen to Twey.
    Last edited by james438; 08-30-2007 at 08:01 AM.

  4. #14
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The single MySQL line that Twey showed you above is all you really need.
    What MySQL line? That was a call to my function

    But yes, in MySQL it's as simple as:
    Code:
    list($value) = mysql_fetch_array(mysql_query('SELECT value FROM yourtable WHERE id = 2099;'), MYSQL_NUM);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #15
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    James superthanks for your help. Very greatful! I might consider mysql but will wait...it is a bit too much for me right now.

    Twey, here is my test code but it doesn't return anything...

    Code:
    <?PHP
    
    
    define('BUF_SIZ', 128);
    
    define('TYPE_COLUMN', 0);
    define('YEAR_COLUMN', 1);
    define('ID_COLUMN', 2);
    define('VALUE_COLUMN', 3);
    
    function select_from_file($get, $where, $val, $multiple = false) {
      $col &#37;= 4;
      $r = $multiple ? array() : '';
      $f = open('file.dat', 'r');
    
      for($current_line = '', $next_line = '', $pos; !feof($file);) {
        while(($pos = strpos($current_line, "\n")) === false)
          $next_line .= fread($f, BUF_SIZ);
        $next_line = str_replace("\r", '', $next_line);
        list($current_line, $next_line) = explode("\n", $next_line, 2);
        if($current_line) {
          $current_line = explode(' ', $current_line);
          if($current_line[$where] == $val)
            if($multiple)
              $r[] = $current_line[$get];
            else {
              $r = $current_line[$get];
              break 2;
            }
        }
      }
      fclose($f);
      return $r;
    }
    
    
    select_from_file('#IB', '0', '2099');
    
    ?>

  6. #16
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You've used it wrong. It takes three arguments, in the order of an SQL query: the column to get, the column to check, and the value that the checked column must have in order to match. The first two arguments should be *_COLUMN constants as defined at the top of the file. You'll also probably need to change 'file.dat' in the function to the name of your text file.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #17
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Got this piece of code from another forum. Any comments in it? (memory perspective etc):

    Code:
    <?PHP
    
    
    $contents = file('file.txt');
    
    foreach($contents as $line_value)
    {
    
    list($cat, $int, $id, $num) = explode("\t", $line_value);
    
        $cat = str_replace('#', '', $cat);
    
        $number[$cat][$int][$id] = trim($num);
    }
    
    //echo '<pre>' . print_r($number, true) . '</pre>';
    
    
    echo $number['UB'][-1][1650]*2;// returns -81219.00
    
    // similarly $number['UB'][-1][1229]; will return 14100.00
    
    ?>

  8. #18
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    As with james438's code, it loads the whole file into memory at once.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #19
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Possible to modify for better memory handling?

  10. #20
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Aa I see. There are no column constants as in categories in my file...Well I have got some working solutions to continue my journey with.


    Quote Originally Posted by Twey View Post
    You've used it wrong. It takes three arguments, in the order of an SQL query: the column to get, the column to check, and the value that the checked column must have in order to match. The first two arguments should be *_COLUMN constants as defined at the top of the file. You'll also probably need to change 'file.dat' in the function to the name of your text file.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •