Perl I/O Learning Objectives:

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

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.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
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.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
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.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
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.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
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,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Guide To UNIX Using Linux Third Edition
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(,,,..., );
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Linux+ Guide to Linux Certification, Third Edition
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
File IO and command line input CSE 2451 Rong Shi.
Introduction to Unix – CS 21
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
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!)
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Linux Administration Working with the BASH Shell.
Strings CSCI 112: Programming in C.
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
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.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Chapter 7 Text Input/Output Objectives
Getting Started with C.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Computer Science 210 Computer Organization
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Computer Science 210 Computer Organization
Programming in C Input / Output.
Perl Variables: Array Web Programming.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Context.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Programming in C Input / Output.
Module 12 Input and Output
Introduction to Perl Learning Objectives:
Perl Process Management
Introduction to C Programming
Karan Thaker CS 265 Section 001
Presentation transcript:

Perl I/O Learning Objectives: To understand how to perform input from standard Input & how to process the input To understand how to perform input from file To understand how to write [formatted] output to standard output

Perl I/O Table of Content Input from STDIN Output to STDOUT Printf formatted Output

More about Sort The sort function returns a sorted array in ascending ASCII order: @a = (1,2,4,8,16,32,64); @b = sort(@a); # (1,16,2,32,4,64,8) If sort by numerical order, you have to tell Perl by including a comparison test “by number” (not ASCII): @c = sort {$a<=>$b} @b # (1,2,4,8,16,32,64)

Input from STDIN (1) Reading from STDIN is easy, and we have done it many times. $a = <STDIN>; In a scalar context, this gives the next line of input, or undef if there are are no more lines (the user has hit CTRL-d). In a list context, this gives all remaining lines (all the lines until the user hits CTRL-d). @a = <STDIN>; Each element is one line, and includes the terminating newline (the last line may or may not have a newline).

Input from STDIN (2) Typically, a program will read in one line at a time and process the line: while(defined($line = <STDIN>)){ # process line print "$line"; } In the above example, defined() returns true unless its argument is undef. As long as a line has been read, <STDIN> returns a defined value, and the loop continues. When <STDIN> has no more lines to read (it hits CTRL-d), it returns undef, terminating the loop.

Input from STDIN (3) Here is an example of program input and output. White text is typed by the user, green text is printed by the program or shell. $ cat line1 #!/usr/local/bin/perl5 -w while(defined($line = <STDIN>)){ # process line print "$line"; } $ line1 hi test [CTRL-d] $

Input from STDIN (4) We can also read and print the lines altogether. Again, white text is typed by the user, green text is printed by the program or shell. $ cat line2 #!/usr/local/bin/perl5 -w @lines = <STDIN>; # process lines print "@lines"; $ line3 hi test [CTRL-d] hi $

Input from STDIN (5) Perl has a shortcut for reading a value from <STDIN> into $_. Whenever a condition consists solely of <STDIN>, Perl automatically copies the line into $_. while(<STDIN>){ # same as: while(defined($_ = <STDIN>)){ chomp; # same as: chomp "$_"; print "$_\n"; } $_ is the default for many operations, such as chomp and print.

Output to STDOUT We have also often used print. The print function takes a list of strings, and sends each to STDOUT, without adding any characters between them. print "Hi Bill!\n"; print "Hi ", "Bill!", "\n"; print("Hi ", "Bill!", "\n"); Sometimes, you will need to add parentheses to print, especially when the first thing starts with a left parenthesis: print (1+1), "cheap Bill\n"; # problem! print ((1+1), "cheap Bill\n"); # ok print 1+1, "cheap Bill\n"; # also ok

printf Formatted Output (1) You may wish to have more control over your output than print provides. The printf function takes a list of arguments, where the first argument is a format control string, which defines how to print the remaining arguments (like printf in C). printf "%s %d %f\n", $s, $n, $real; Format types: strings %s integer numbers %d floating-point numbers %f

printf Formatted Output (2) $ cat printf1 #!/usr/local/bin/perl5 -w $s = "1234567890"; $n = 12345; $real = 1234567.123; printf "%15s %5d %10.2f\n", $s, $n, $real; 1234567890 12345 1234567.12 $ This example prints $s in a 15-character field, then space, then $n as a decimal integer in a 5-character field, then another space, then $real as a floating-point value with 2 decimal places in a 10-character field, and finally a newline.