Sort a text file in perl




















Active 8 years, 6 months ago. Viewed 2k times. Improve this question. Anass Anass 5, 6 6 gold badges 25 25 silver badges 35 35 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. End with another map statement that turns our arrays back into the original lines. Your regex is wrong.

Stephen J. Anderson Stephen J. Anderson 3 3 silver badges 6 6 bronze badges. How about -2 instead of 5? Works just as well.

Agreed on the first part and changed accordingly. I mildly disagree on the quantifier, but as we're already talking about fictional situations that haven't been described by the OP, I'll not defend myself on the quantifier issue.

Join your peers on the Internet's largest technical computer professional community. It's easy to join and it's free. Register now while it's still free!

Already a member? Close this window and log in. Join Us Close. Join Tek-Tips Forums! Join Us! By joining you are opting in to receive e-mail. Promoting, selling, recruiting, coursework and thesis posting is forbidden. Here is an example of a program that reads a file, capitalizes each line, and prints it to the screen:.

Sometimes it's easier to read a whole file into a list, especially with complex break logic, read-ahead totals, or sorting.

Here's a program that reads a file and prints it in sorted order:. Perl is exceptionally good at file conversion. For simplicity, assume names have no spaces no Mary Anns, no Van Gelders. Here's a 21 line program to do the conversion:. Copyright C by Steve Litt -- Legal. File Slurping You might occasionally want to grab an entire file without paying attention to line termination. This is called "slurping" the file. The following code slurps the STDIN file, then splits it into lines, then reassembles the lines into a single string, and prints the string: x!

Now we undef the line terminator character Now we slurp the entirety of STDIN Now we restore the line terminator character Now we split the string we read using the termator as a border Now we join the array back into a string We print the string Last but not least, we print an extra newline to fix a picket fence condition Slurping isn't as handy as it might seem. Not in Perl! For whatever reason, line oriented is faster.

One reason is the need for huge amounts of memory, which on UNIX systems translates into huge disk usage as swap file space is used. But this doesn't account for the whole thing, as you'll see in the test following program:! A line at a time input that pushed on an array and then outputted it a line at a time took 74 seconds.

Note that this stores the full file in memory. The slurp method, which reads the file into a string and then copies it to an array, takes seconds. Looking more closely, the slurp version actually has two copies of the file in memory -- one in the array and one in the scalar. If the file is large enough to save time by whole file reads, then it's so large as to exhaust electronic RAM memory, thus incurring swap penalties. The most efficient algorithm reads a line, writes a line, and stores nothing.

That's not always practical, and it's certainly not the easiest way to design code.



0コメント

  • 1000 / 1000