Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #279 Read list of strings from the standard input

Read all the lines (until EOF) into the list of strings lines.

@lines = <STDIN>;
with Ada.Text_IO;
with Ada.Containers.Indefinite_Vectors;
declare
   package String_Vectors is new
      Ada.Containers.Indefinite_Vectors
         (Index_Type => Positive, Element_Type => String);
   use String_Vectors, Ada.Text_IO;
   Lines : Vector;
begin
   while not End_Of_File loop
      Lines.Append (Get_Line);
   end loop;
end;
import "bufio"
import "os"
var lines []string
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
	line := s.Text()
	lines = append(lines, line)
}
if err := s.Err(); err != nil {
	log.Fatal(err)
}
import static java.lang.System.in;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;
var is = new BufferedInputStream(in);
List<String> lines = new ArrayList<>();
String s = "";
int c, eof = -1, n = 0xf;
final int cr = '\r', lf = '\n';
while ((c = is.read()) != eof)
    switch (c) {
        case cr, lf:
            lines.add(s);
            s = "";
            if (c == cr) {
                is.mark(n);
                if (is.read() != lf)
                    is.reset();
            }
            break;
        default:
            s += (char) c;
    }
lines.add(s);
uses classes;
while not EOF do
begin
  readln(s);
  lines.Add(s);
end;
import sys
lines = sys.stdin.readlines()
from sys import stdin
lines = [*stdin]
from sys import stdin
lines = [x.rstrip() for x in stdin]
lines = readlines
use std::io::prelude::*;
let lines = std::io::stdin().lock().lines().map(|x| x.unwrap()).collect::<Vec<String>>();

New implementation...