好的,情况看起来很简单,那我在做什么错了?
$fname = "students.dat";
$f_handle = fopen("data/$fname", 'r');
$num_lines = count(file("data/$fname"));
for($x=0; $x<$num_lines; $x++) {
$line = fgets("data/$f_handle");
echo "Line $x: ($line)<br />"; //exit;
if(strpos($line,$studentID)) {
// Line has studentID
echo "Line $x: ($line)($studentID)<br /> <br />";
// Exit the loop if found
break;
}
我的输出是这样:
Line 0: ()
Line 1: ()
Line 2: ()
Line 3: ()
Line 4: ()
Line 5: ()
我使用平面文件,因为我的MySQL尝试失败,$ studentID是正确的,并且数据文件有六行。有什么建议吗?
答案 0 :(得分:2)
library(microbenchmark)
microbenchmark(m1 = {ind <- expand.grid(1:NRows, 1:NRows);
matrix(map2_dbl(ind$Var1, ind$Var2, ~sum((myMat[.x,] - myMat[.y,])^2)),
NRows,NCols)},
m2 = {ind <- expand.grid(1:NRows, 1:NRows);
myfun <- function(x, y) sum((myMat[x,] - myMat[y,])^2);
myfun_v <- Vectorize(myfun);
matrix(myfun_v(ind$Var1, ind$Var2), NRows, NCols)},
m3 = {for (i in 1:nrow(myMat)){
for (j in 1:nrow(myMat)){
norm <- matrix(0, NRows, NCols);
norm[i,j] <- sum((myMat[i,]-myMat[j,])^2)
}}},
m4 = {dist(myMat, diag = TRUE, upper = TRUE)^2}
)
Unit: microseconds
expr min lq mean median uq max neval cld
m1 261.142 278.3625 310.1212 302.5230 323.8570 434.894 100 b
m2 200.997 238.7805 287.5953 259.5995 286.3305 2417.610 100 b
m3 4902.562 5369.8390 5725.5358 5578.2895 5717.8560 10317.123 100 c
m4 16.451 22.6200 34.1653 32.9010 42.4110 81.222 100 a
包含一个资源对象(刚刚打开的文件的句柄)。如import java.util.Collections;
import java.util.List;
public class NodeSorter {
public void sort(List<Node> nodes) {
Collections.sort(nodes, (x, y) -> Character.compare(x.getId(), y.getId()));
for (int i = 0; i < nodes.size() - 1; i++) {
for (int j = i + 1; j < nodes.size(); j++) {
Node x = nodes.get(i);
Node y = nodes.get(j);
if (y.getChildren().contains(x)) {
nodes.remove(y);
nodes.add(i, y);
i--;
break;
}
}
}
}
}
中那样,将其插入字符串是没有意义的。只需使用@Override
public int compare(Node o1, Node o2) {
if (o1.getChildren().contains(o2)) {
return -1;
}
if (o2.getChildren().contains(o1)) {
return 1;
}
return Character.compare(o1.getId(), o2.getId());
}
。
除此之外,您的代码中还有其他一些问题:
add_shortcode('custom_acf',custom_acf_callback);
function custom_acf_callback ($atts = '')
{
$value = shortcode_atts( array(
'my_field' => '',
), $atts );
// the main part starts from here:
if ($custom_acf_value) {
return $custom_acf_value;
}
}
之类的东西。$f_handle
返回"data/$f_handle"
,但是如果在干草堆字符串的开头找到needle子字符串,它也会返回fgets($f_handle)
。两者在$x = 0; while(($line = fgets($f_handle)) !== false) { ... ; $x++; }
条件下均表现为strpos()
。您应该直接与false
进行比较:0
。false
可能在这里也不起作用。假设您正在搜索ID为if
的学生,但是您正在扫描的当前行的ID为false
。您的测试将在该行错误地匹配。if(strpos(...) !== false)
后面有一个大括号,但没有匹配的大括号。