5 ms·
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:
by imaginenore 10y 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);
- matthewking 10y agoMost 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();
- imauld 10y agoAnd 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.
- astrodev 10y agoEven simpler, and handles the different new line characters: $l = file('in.txt', FILE_IGNORE_NEW_LINES);
- dotancohen 10y agoIf 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.html https://www.cs.purdue.edu/homes/cs290w/perlLecs/PerlPatterns...
- herbst 10y agoJust 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?
- imaginenore 10y agoIndeed, Ruby is a beautiful language, but so ridiculously slow.
- herbst 10y agoWhile i would say thats a non issue in webdevelopment and scripting, i agree that compared to others its just slow