Hacker News new | ask | show | jobs
by imaginenore 3448 days ago
I know PHP isn't very popular here, but it's awesome for string manipulation, parsing files.

One-line that reads the file, splits multiple lines into an array:

    <?php 
        $l = explode("\n", file_get_contents("in.txt"));
Want to sort them alphabetically? Add

    sort($l);
Trim them?

    $trimmed = array_map(trim, $l);
Remove duplicates?

    $unique = array_unique($trimmed);
4 comments

If you are turning to PHP for string manipulation, then you are going to love the Practical Extraction and Reporting Language: https://www.cs.purdue.edu/homes/cs290w/perlLecs/PerlPatterns...
Most languages have these type of functions, once you've read the file into an array its just array/list manipulation. I haven't compile checked this but here's the same as a C# one liner:

List<string> lines = File.ReadAllLines("/file.txt").Distinct().ToList().Select(l => l.Trim()).Sort();

And in Python:

with open("foo.txt.") as f: foo = sorted(list(set(line.strip() for line in f])))

with is a context manager that will automatically close the file once it's out of scope, not required but it's much cleaner and safer. Should the code change you don't have to worry about the call to close the file getting lost. The call to set() could be replaced with a set comprehension, {i for i in sequence}, but for clarity I used set().

It's certainly not the most efficient way to do it but it's simple and clear.

Just FYI why i lold at this beeing a fitting example:

  File.read("in.txt").split("\n").sort.map(&:trim).uniq
No need for X temporary variables and N lines of code in ruby.

Also notice the better readability as everything happens from left to right?

Indeed, Ruby is a beautiful language, but so ridiculously slow.
While i would say thats a non issue in webdevelopment and scripting, i agree that compared to others its just slow
Even simpler, and handles the different new line characters:

  $l = file('in.txt', FILE_IGNORE_NEW_LINES);