Input/Output for lines of text: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Python}}: Python 3 & 2)
Line 37: Line 37:
<lang perl6>$*OUT.say($*IN.get) xx $*IN.get;</lang>
<lang perl6>$*OUT.say($*IN.get) xx $*IN.get;</lang>
=={{header|Python}}==
=={{header|Python}}==
<lang python>def do_stuff(word):
<lang python>try: input = raw_input
except: pass
print(word)


def do_stuff(words):
t = input()
print(words)
for x in range(0, t):

word = raw_input()
linecount = int(input())
do_stuff(word)</lang>
for x in range(linecount):
line = input()
do_stuff(line)</lang>


=={{header|REXX}}==
=={{header|REXX}}==

Revision as of 07:16, 9 January 2014

Input/Output for lines of text is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The first line contains the number of lines to follow, followed by that number of lines of text on STDIN. Output to STDOUT each line of input by passing it to a method as an intermediate step. The code should demonstrate these 3 things. See also Input/Output for Pairs of Numbers and File/Input and Output.

Sample input with corresponding output

Input

3
hello
hello world
Pack my Box with 5 dozen liquor jugs 

Output

hello
hello world
Pack my Box with 5 dozen liquor jugs

Java

<lang java>import java.util.Scanner;

public class Main { public static void doStuff(String word){ System.out.println(word); }

public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); //doesn't use nextInt() so nextLine doesn't just read newline character for(int i=0; i<n; i++){ String word = in.nextLine(); doStuff(word); } } }</lang>

Perl 6

<lang perl6>$*OUT.say($*IN.get) xx $*IN.get;</lang>

Python

<lang python>try: input = raw_input except: pass

def do_stuff(words): print(words)

linecount = int(input()) for x in range(linecount): line = input() do_stuff(line)</lang>

REXX

Programming note:   this method was chosen because the standard input may be identical to the standard output. <lang rexx>/*REXX program outputs a number of lines from the default input file. */

  1. =linein() /*the number of lines to follow. */
 do j=1  for #;   x.j=linein();  end  /*obtain input lines from stdin. */

call stuff /*call STUFF routine for output. */ exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────STUFF routine───────────────────────*/ stuff: do k=1 for #; call lineout ,x.k; end; return /*output.*/</lang>

Ruby

<lang ruby>def do_stuff(word) puts word end

t = gets.to_i for i in 1..t do word = gets do_stuff(word) end</lang>