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,

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.
More Perl Control Flow Software Tools. Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other.
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.
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 Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
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.
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.
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.
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
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
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.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
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.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
Linux+ Guide to Linux Certification, Third Edition
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
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.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Introduction to Unix – CS 21
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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 “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
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.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Files A collection of related data treated as a unit. Two types Text
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Linux Administration Working with the BASH Shell.
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Getting Started with C.
Programming in C Input / Output.
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
Perl Variables: Array Web Programming.
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 Input and Output.
Perl I/O Learning Objectives:
Module 12 Input and Output
Introduction to Perl Learning Objectives:
Perl Process Management
Presentation transcript:

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, 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 = ; Each element is one line, and includes the terminating newline (the last line may or may not have a newline).

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

Lecture 15 / Slide 4 Input from STDIN l Here is an example of program input and output. l 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 = )){ # process line print "$line"; } $ line1 hi test [CTRL-d] $

Lecture 15 / Slide 5 Input from STDIN l We can also read and print the lines altogether. l Again, white text is typed by the user, green text is printed by the program or shell. $ cat line2 #!/usr/local/bin/perl5 = ; # process lines print $ line3 hi test [CTRL-d] hi test $

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

Lecture 15 / Slide 7 Input from <> Another way to read input is with <>. <> returns a single line in a scalar context, or all the remaining lines in a list context. However, unlike, <> gets its data from the file (or files) specified on the command line. $ cat f1 1 $ cat f2 2 $ cat mycat #!/usr/local/bin/perl5 -w while(<>){ print; } $ mycat f1 f2 1 2 $

Lecture 15 / Slide 8 Input from <> If you do not specify any filenames on the command line, <> reads from standard input automatically. $ cat mycat #!/usr/local/bin/perl5 -w while(<>){ print; } $ mycat hi test [CTRL-d] $ <> is the easiest way to read files in Perl (we will learn more advanced ways later).

Lecture 15 / Slide 9 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

Lecture 15 / Slide 10 printf Formatted Output 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; l Format types: strings%s integer numbers%d floating-point numbers%f

Lecture 15 / Slide 11 printf Formatted Output $ cat printf1 #!/usr/local/bin/perl5 -w $s = " "; $n = 12345; $real = ; printf "%15s %5d %10.2f\n", $s, $n, $real; $ printf $ 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.