Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
COMP234 Perl Printing Special Quotes File Handling.
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
5.1 Previously on... PERL course (let ’ s practice some more loops)
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
An Introduction to Unix Shell Scripting
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Input, Output, and Processing
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
13 More Advanced Awk Mauro Jaskelioff (originally by Gail Hopkins)
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Content Programming Overview The JVM A brief look at Structure
Input from STDIN STDIN, standard input, comes from the keyboard.
Formatting Output.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Basic Elements of C++.
Python’s input and output
Introduction to C CSE 2031 Fall /3/ :33 AM.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Formatting Output.
Command Line Arguments
Programming in C Input / Output.
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Handling.
Perl I/O Learning Objectives:
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if the command line (following the $ prompt symbol) is: t80maj1 $ print_input < input_file.txt the program “print_input” will take each line from the file “input_file.txt” and process in a loop such as: while ( ) { print; } = ; A special note: the while ( ) construction assigns the output of STDIN to $_, exactly the same as while ($_ = ). But, you can’t put anything else in the conditional clause (such as “chomp”, or a test condition, and still get assigned to $_.

Input from Files There are 2 ways of using a file as input instead of using the keyboard. The simplest way mimics the behavior of standard Unix commands like “ls” and “cat”. On the command line, the first word in the Perl program to run. All words following this are treated as the names of files to be opened and processed by the Perl program: t80maj1 $ print_files.pl file1.txt file2.txt This syntax can be used to open and process each of these files. To get this to work, your Perl program needs to use the “diamond operator” <>: while (<>) { print; } If there is no filename on the command line after the program’s name, the program processes keyboard input instead. Also if a hyphen is used instead of a file name.

File Handles The best way to open a file whose name isn’t going to change each time you run the program is to open it from within the program. The “open” command assigns a file name to a file handle, which is then used like STDIN is: open INFILE, “my_file.txt”; while ( ) { print; } Files are closed automatically when the program terminates, but sometimes you need to specifically close them: close INFILE; # or whatever the file handle’s name is

Writing and Appending to Files Files can also be opened for writing or appending. Writing to a file means either creating it or opening a pre- existing file and destroying all of its contents. To write to a file, use a “>” before the file’s name in the “open” command: open OUTFILE, “>my_file.txt”; To actually write to this file, use the file handle’s name with “print”: print OUTFILE “something interesting\n”; Appending to a file means creating a new file if it doesn’t exist, or adding new contents to the end of a pre-existing file. Appending is done by using “>>” in front of the file’s name: open APPENDFILE, “>>my_file.txt”;

Command Line Arguments You can pass arguments into a Perl program with the command line. On the command line, this looks just like the way we used the diamond operator: t80maj1 $ print_files.pl file1.txt file2.txt The difference is internal: instead of using “<>”, All words on the command line after the programs’ name are put which can then be used just like any other array: foreach my $arg { print “$arg\n”; } Note that there is no equivalent to C’s “argc” variable, which gives the number of arguments. To get this number, simply in a scalar context: print “The number of arguments is: “, “\n”;

Formatted Printing C uses the “printf” command to produce almost all of its output. C++ uses “cout” instead. Occasionally it is necessary to format Perl output. For instance, this is the easiest way to round off numbers to a specified number of decimal places, or to put numbers into neat columns. The syntax for “printf” uses a format code that starts with %, in quotes, followed by one or more things to print: printf “%g”, 54; This would simply print “54”. The basic format codes: -- “%d” prints integers. Non-integral values are truncated (not rounded): printf “%d”, ; gives “99”. -- “%g” prints in the “general format. Perl automatically chooses between integer, decimal, and exponential formats: printf “%g”, 5/2, 6/2, 6 * 10**23; # 2.5, 3, 6.0e+23 --”%f” prints in floating point format, with a decimal point --”%s” means string format.

More printf The number of columns used to print, as well as justification, can be selected: printf “%6d”, 12; prints the number 12 right-justified with a total of 6 spaces (the leftmost 4 will be blanks). With “%f”, the number of positions after the decimal point can be specified: printf “%6.3”, ; gives That is, a total of 6 spaces (including the decimal point), with 3 after the decimal point. This is how numbers are rounded; note the “7” at the end. “%f” rounds the numbers instead of truncating them as “%d” does. Left justification can be done using negative numbers in the format code: printf “%6d”, 12; # 4 blank spaces followed by 12 printf “%-6d, 12; # 12 followed by 4 blank spaces

Still More printf You can include other text inside the quotes with the format code: printf “The cat has %d lives\n”, 9; prints “The cat has 9 lives” To get an actual percent sign, use “%” printf “Half means %5.3f%”, 50; prints “Half means 50.00%” To print to a string instead of to output, use “sprintf” with the format codes: my $str = sprintf “Half means %5.3f%”, 50; puts “Half means 50.00%” into the variable $str.