java while 循环中隐式变量的名称是什么?
在 Perl 中,您可以编写类似的内容
while(!eof){
doSomething(x,y);
print $_;
}
,第二条语句使用 $_
打印出循环所进行的迭代,这是 Perl 用于迭代循环的不可见变量的名称。 Java 中该变量的名称是什么?
我意识到您可以通过在循环外部声明一个变量并递增它来完成同样的事情,但如果有人知道如何做到这一点,这种方式肯定会更优雅。
In Perl you can write something like
while(!eof){
doSomething(x,y);
print $_;
}
and the second statement prints out the iteration the loop is up to using $_
, the name of the invisible variable Perl uses to iterate through the loop. What is the name of that variable in Java?
I realise that you can accomplish the same thing by declaring a variable outside the loop and incrementing it, but this way is definitely more elegant if anyone knows how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 中没有像 Perl 中那样的隐式变量。你只需要在 Java 中更加明确...
There's no implicit variable in Java as in Perl. You just have to be more explicit in Java...
Java中没有这个概念。
在 Groovy 中,至少您可以在函数方法上设置一个
it
变量。或读取文件,就像在您的示例中一样......
There is no such concept in Java.
In Groovy, at least you get an
it
variable set on the functional methods.or to read a file, like in your example...
你错了; perl 不会在您显示的 while 循环中隐式对 $_ 执行任何操作。
您要么考虑 for/foreach 循环,其中 $_ 是默认迭代变量(如果您未指定),要么考虑 while 的特殊情况,其中表达式是 readline 或 glob 操作,其中对
的赋值$_
已为您添加。You are mistaken; perl does not implicitly do anything with $_ in the while loop you show.
You are either thinking of for/foreach loops, where $_ is the default iteration variable if you do not specify one, or of the special case of a while with the expression being a readline or glob operation, where an assignment to
$_
is added for you.你甚至可以更加含蓄:
然而,曾经可以接受的东西现在已经不再可以接受了。这很像在代码中不使用
use strict
和use warnings
。不使用这些编译指示可以让您做很多有趣的事情。您可能不打算做的事情以及使您的代码很难理解的事情。几乎所有 Perl 程序员都不再使用隐式的
$_
和@_
因为它只会让你的代码更难理解并且可能导致错误(比如假设while
还使用隐式$_
;-) )。Java(和大多数语言)没有这样的默认变量,因为这并不是一个好主意。像 CPAN 和扩展正则表达式这样的 Perl 优秀思想现在已经被复制到其他语言中。像默认变量这样的东西还没有。
使用这些东西会很有趣——就像一个秘密解码器环,让您知道谁是真正的 Perl 黑客,谁不是。通读所有混乱的 Perl 代码并看看谁真正理解 Perl 的内部工作原理是很有趣的。
然而,它也为其他语言的开发人员提供了他们所需的证据,证明 Perl 极其草率且无法理解。结果,Perl 的接受度受到了影响。
you can even be more implicit:
However, what was once acceptable is no longer acceptable. It's much like not using
use strict
anduse warnings
in your code. Not using these pragmas allowed you to do so many interesting things. Things that you might not have intended to do and things that make your code really hard to understand.Almost all Perl programmers no longer use the implicit
$_
and@_
because it's just makes your code all that more difficult to understand and can be a cause errors (like assuming thatwhile
also uses an implicit$_
;-) ).Java (and most languages) don't have such a default variable because it's not really all that good an idea. Good Perl ideas like CPAN and expanded regular expressions have now been copied in other languages. Things like default variables have not.
Using this stuff can be fun -- like a secret decoder ring that allowed you to know who's a real Perl hacker and who isn't. It's fun to read through all that obfuscated Perl code and see who really understands the inner workings of Perl.
However, it has also given developers in other languages the proof they need that Perl was hopelessly sloppy and impossible to understand. And, as a result, Perl has suffered in general acceptance.