I want to scanf, dammit!


The Perl sages say: "scanf sucks, we don't need it". Well, here is a real-life situation that I have to deal with quite frequently.

A file with floats separated by spaces and newlines. There can be several numbers per line, but I don't know and don't care about exactly how they are written. Suppose the file contains 2500 numbers and I need to read the first 500, or better yet, read just the number #500. This number can be in the middle of a line, for all I know.

Using something like scanf or the C++ stream input operator, the solution is ludicrously simple: just read the requisite number of times in a loop using a single statement. But with Perl I just don't see a simple way to do it that does not involve slurping the entire file into memory first).

Dmitry Epstein's original post