import System.Environment
import Data.List
main = do
infile:outfile:_ <- getArgs
input <- readFile infile
writeFile outfile . unlines . sort . lines $ input [infile, outfile] <- getArgs
Also, the last two lines may be squeezed into one (though less readable): writeFile outfile =<< (unlines . sort . lines) `fmap` readFile infileOn your second point, I actually thought about writing the whole thing as
getArgs >>= \a -> readFile (a!!0) >>= writeFile (a!!1) . unlines . sort . lines
but decided that that's exactly the kind of thing that gets Haskell programmers a bad reputation. use strict;
use warnings;
open my $read,"<",$ARGV[0] or die $!;
my @lines = <$read>;
close $read;
open my $write,">",$ARGV[1] or die $!;
print $write $line foreach my $line(sort @lines);
close $write; use v5.12; # enables strictures
open my $in, "<", $ARGV[0];
open my $out, ">", $ARGV[1];
print $out sort <$in>;
Perl6: sub MAIN( $in, $out ) {
spurt( $out, open($in, chomp => False).lines.sort.join )
}
[EDIT: fixed typo in first example] my @files = map { IO::File->new($_->[0], $_->[1]) || die $! }
([shift, 'r'], [shift, 'w']);
$files[1]->print( sort $files[0]->getlines );
$_->close for @files;
And... use autodie;
open my $out, '>', $ARGV[1];
print {$out} sort do {open my $in, '<', $ARGV[0]; <$in>}; array = IO.readlines ARGV[0]
array.sort!
File.write ARGV[1], array.join("\n")
edit: get input and output file names from command line lines = IO.readlines ARGV[0]
File.write ARGV[1], lines.sort.join('\n')
certainly more readable than the C++ even if you're unfamiliar with ruby. File.write ARGV[1], IO.readlines(ARGV[0]).sort.join('\n')
;) IO.write(ARGV[1], ARGF.lines.sort.join) IO.write$*.pop,ARGF.lines.sort.join
And yes, it does work even without the space between `write` and `$*`.Also after testing, I realized the ('\n') is not required for join. When you call 'lines', it still has the '\n' character in the string, and when you join, it defaults to join without a delimiter, so it's putting them back together with the newline still there.
Maybe this would fix it:
IO.write(ARGV.pop, ARGF.lines.sort.join('\n'))Also, trim out the space between the arguments and kill the parenthesis for the optimal golfing.
> File.write ARGV[1], (IO.readlines ARGV[0]).sort.join('\n')
not valid Ruby?
File.open(ARGV[1], 'w') { |file| file.write(File.read(ARGV[0]).lines.sort.join) }
http://stackoverflow.com/questions/8278680/ruby-undefined-me...BTW, I would never use Ruby for anything that needs performance, but for ease of use and readability it's really great.
ruby -e 'puts ARGF.sort' file.txt
Also, this reads stdin if the argument is omitted.It's actually really hard to completely rewrite that program in most scripting languages: they tend not to have the same concept of undefined behaviour.
'sys.argv[2]' in python (with sys.argv = ['thing.py']) is fully defined (raises an IndexError). 'argv[2]' in C++ (with argv = (char*[2]) { "./thing", 0 }) is undefined.
If you scripting language has an FFI library (ctypes in python, say) then you could probably do something equivalent.
import sys
data = open(sys.argv[1]).readlines()
data.sort()
open(sys.argv[2],'w').write(''.join(data)) sorted_lines = sorted(open(sys.argv[1]))
open(sys.argv[2]).writelines(sorted_lines) function Sort-File ($Path, $Destination) {
Get-Content $Path | Sort-Object | Out-File $Destination
}
Granted, shells are probably at an advantage when it comes to file management. I'd think a bash variant would be quite short as well.Aiming for succinctness by using aliases and using redirection instead of cmdlets for writing the file (shorter, but less flexible):
function sf { gc $args[0] | sort > $args[1] }
That's not code anyone should write outside code golfing, though ;-) cat in.txt | sort > out.txtsort in.txt > out.txt
cat file | command > out
is the same as command < file > out sort in.txt > out.txtcat $1 | sort > $2
<?
$lines=file($argv[1]);
sort($lines);
file_put_contents($argv[2],implode("\n",$lines)); sort #include <iterator>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std; // Bad but shorter!
int main(int argc, char** argv) {
ifstream infile(argv[1]);
ofstream outfile(argv[2]);
ifstream_iterator<string> start(infile), end;
vector<string> lines(start, end);
sort(lines.begin(), lines.end());
copy(lines.begin(), lines.end(), ostream_iterator<char>(outfile, "\n"));
}ifile, line and data have to be defined before the loop. ofile could be defined closer to usage, but I think it's much clearer where it is, grouping in and out defs together making it obvious how argv is used for each.
Rebol []
files: map-each n system/options/args [to-file n]
write/lines files/2 sort read/lines files/1
Small nitpcik to the author: please define variables when you're going to use them, not C-style all at the beginning of the function. It makes code easier to understand. It doesn't make the reader wonder 'hey what's this variable going to be used for' then having to crawl through all code underneath it. It creates code that's easier to refactor. Also, but more arguably for such a small sample: http://stackoverflow.com/questions/1452721/why-is-using-name...