简体   繁体   English

如何递增变量,以便它在While循环中从文本文件中选择不同的行

[英]How do I increment a variable so it chooses differnet lines from a text files in a While Loop

I have a script im writing. 我有一个正在写的脚本。 Here is whats happening. 这是怎么回事。 There is a while loop. 有一个while循环。 In the while loop is a variable which is constant to X. How do i make X change from line one, line two, etc for each cycle of the while loop and pull X from a .txt file. 在while循环中,变量是X的常量。我如何在while循环的每个循环中从第一行,第二行等更改X并从.txt文件中提取X。 Everything is in root. 一切都扎根。 Thanks 谢谢

$f = fopen("some.txt", "r");
while (!feof($f) && $some_condition) {
    $x = fgets($f);
    // do something
}
fclose($f);

Would this be sufficient? 这样就足够了吗?

Here is the pseudo code captain Kirk: 这是伪代码上尉柯克:

//we assume current working directory is root
fileHandle = openFile("Read","some.txt");
X = pull("X",fileHandle);

while( X is constant )
{
  XFactor = factor(X);
}

I can refine and improve this with more details about what universe you are from, the programming language you intend to use, and more specifics about what you want to happen. 我可以使用有关您来自什么Universe,您打算使用的编程语言以及您要发生的事情的更多详细信息的更多详细信息来完善和改进此功能。

//get the lines of the file into an array
$file_array = file($file_name);

//go through the array line by line
foreach ($file_array as $line_number => $line)
{
  //you didn't tell us what you are doing with each line
  //so you will need to change this to your liking
  $X = $line; // Handle the line
}

Edit: Note for very large files this may not be a good approach because this will load the entire file into memory at one time. 编辑:请注意,对于非常大的文件,这可能不是一个好方法,因为这会将整个文件一次加载到内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM