Logo

Programming-Idioms

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

Idiom #148 Read list of integers from stdin

Read a list of integer numbers from the standard input, until EOF.

(read)
#include <vector>
#include <iostream>

using namespace std;
vector<int> v;
for(int t;;){
	cin >> t;
	if(cin.eof())
		break;
	v.push_back(t);
}
string input = Console.ReadLine();
string[] intlist = input.Split(new char[] {',', ' '});
	
foreach(string item in intlist)
{
  Console.WriteLine(Convert.ToInt32(item));
}
import std.stdio;
import std.conv : to;
import std.algorithm.iteration : map;
auto list = stdin.byLine.map!(a => a.to!int);
integer :: I,j,k ,l(3)
read(*,*) I, j, k, l
import (
	"bufio"
	"os"
	"strconv"
)
var ints []int
s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanWords)
for s.Scan() {
	i, err := strconv.Atoi(s.Text())
	if err == nil {
		ints = append(ints, i)
	}
}
if err := s.Err(); err != nil {
	return err
}
read <$> getLine :: IO [Integer]

-- reading space separated list of ints
map read . words <$> getLine :: IO [Int]
process.stdin.on('data', processLine)

function processLine (line) {
  const string = line + ''
  console.log(string)
}
const ints = await new Promise(resolve => {
  let string = ''
  process.stdin
    .on('data', data => string += data.toString())
    .on('close', () => resolve(string.split('\n').map(line => Number.parseInt(line))))
})
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
List<Integer> list = new ArrayList<>();
Scanner in = new Scanner(System.in);
while(in.hasNext())
    list.add(in.nextInt());
import java.util.Scanner;
Scanner s = new Scanner(System.in);
int a[] = s.tokens()
    .mapToInt(Integer::parseInt)
    .toArray();
s.close();
fgl
type
  TFloatList = specialize TFPGList<Double>;
var
  List: TFloatList;
  D: Double;

begin
  List := TFloatList.Create;
  while not Eof do
  begin
    Read(D);
    List.Add(D);
  end;
  List.Free;
end.
@x = map {chomp; $_} <>;
list(map(int, input().split()))
numbers = [int(x) for x in input().split()]
from itertools import chain
from sys import stdin
a = (x.split() for x in [*stdin])
a = map(int, chain(*a))
STDIN.read.split.map(&:to_i)
use std::{
    io::{self, Read},
    str::FromStr,
}
let mut string = String::new();
io::stdin().read_to_string(&mut string)?;
let result = string
    .lines()
    .map(i32::from_str)
    .collect::<Result<Vec<_>, _>>();

New implementation...