2014-12-11

Language Comparison

Perl:

while (<>) { print "$. : $_" }

Perl:

while (<>) {
  print "$. : $_"
}

CSharp:

using System;
using System.IO;

class App {
	public static void Main(string[] args) {
		int line_number = 1;
		foreach (string arg in args) {
			foreach (string line in File.ReadLines(arg)) {
				Console.WriteLine(line_number + ":" + line);
				line_number++;
			}
		}
	}
}

Python:

import sys

for arg in sys.argv[1:]:
	for index, line in enumerate(open(arg).readlines()):
		print(index + 1, ":", line, end='')

Golang:

package main

import (
	"fmt"
	"os"
	"bufio"
)

func main() {
	line_number := 1
	for _, arg := range os.Args[1:] {
		file, _ := os.Open(arg)

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			fmt.Printf("%d : %s\n", line_number, scanner.Text())
			line_number++;
		}
	}
}

Java:

import java.io.*;
import java.util.*;

class Linum {
        public static void main(String[] args) throws FileNotFoundException {
                int line_number = 1;
                for (String arg : args) {
                        Scanner s = new Scanner(new BufferedReader(new FileReader(arg)));

                        while (s.hasNextLine()) {
                                System.out.println(line_number + " : " + s.nextLine());
                                line_number++;
                        }
                }
        }
}

Ruby:

while gets
  puts "#{$.} : #{$_}"
end
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s