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.

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

Lecture 2 Introduction to C Programming
Introduction to C Programming
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
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 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.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
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.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introduction to Unix – CS 21
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
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.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Random Bits of Perl None of this stuff is worthy of it’s own lecture, but it’s all a bunch of things you should learn to use Perl well.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
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.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
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 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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Files A collection of related data treated as a unit. Two types Text
C is a high level language (HLL)
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Input from STDIN STDIN, standard input, comes from the keyboard.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C Input / Output.
Programming in C Input / Output.
Perl Variables: Array Web Programming.
Perl I/O Learning Objectives:
Programming in C Input / Output.
Conversion Check your class notes and given examples at class.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

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 you an undef value when you reach eof – handy for dropping out of loops while (defined($line = )) { print “I saw $line”; }

Shortcut We’re reading the input into a variable – checking to see if it’s reached the eof (meaning its defined) – if its defined we run the while loop. Inside the loop – we’ll see each line one after another. Since this is something that is done quite often – Perl has a shortcut. while ( ) { print “I saw $_”; } This only works if you put a line-input operator in the condition of the while loop.

List context The previous examples were evaluated in scalar context Now for list context: foreach ( ) { print “I saw $_”; } gives you all the remaining lines as a list – each element of the list is one line

What’s the difference A while loop in Perl reads a line of input – puts it into a variable – runs the body of the loop. Then it goes back to fine another line of input. A foreach loop --- the line-input operator is being used in a list context – foreach needs a list to iterate through – so it has to read all the input before the loop starts running. For large files – its best to use the while loop for better performance.

Input from Diamond Operator is another way to read input it uses invocation arguments to accept input invocation arguments are command line arguments Exp: –$./myprogram.pl x y z –means to run myprogram and it should process file x followed by file y and then file z –If you give it no invocation args – program should process the standard input stream or –x means stdin as well.

Benefits you can choose where the program gets its input at run time exp: you won’t have to rewrite the program to use it in a pipeline ( more later). makes it easy for you to write your programs that work like standard Unix utilities – even on non-Unix machines. portability of code

Exp: while ( defined($line = <>)){ chomp ($line); print “It was $line that I saw \n”; } run this with x, y, z invocation args – It was [a line from file x] that I saw -- eof then y – then z -- no break with using the <> its as if all the files are merged into one.

Same Shortcut may also use the same shortcut: while (<>){ chomp; print “It was $_ that I saw!\n”; } Typically used for all of your input – mistake to use it in more than one place in your program.

Unix Invocation Arguments Typically, the diamond operator isn’t looking at the invocation args – its works with array. It is a special array – preset by Perl interpreter to be a list of command line args. When program is stuffed full of the list of invocation args. Use it like any other array – shift things off or foreach through it. If list is empty -- <> looks uses STDIN stream – otherwise it uses contents of that array

Tinker Exp: process three specific files – regardless of user choice at = qw! Monday Tuesday Wednesday !; while (<>) { chomp; print “It is $_ \n”; }

Output to Standard Output print operator takes a list of values and sends them – as a string – to stdout – one after another. doesn’t add any extra characters before, after or in between by default. perlvar man page gives you more information about changing defaults if you want spaces between items and a newline at the end – you have to add them

Output - STDOUT $name = “Bob Barker”; print “Hello there, $name, did you know that 4*5 is “, 4*5, “?\n”; difference between printing an array and interpolating an array: #print a list of items print #print a string (containing and interpolated array)

Print Statment First print statement -- one item after another no spaces Second statement will print exactly one item, which is the string you get by into the empty string – the contents of the array separated by spaces.

Print Cont’d But what is a list of unchomped lines of input? Contain trailing newline character First print statement – Monday Tuesday Wednesday Second one: Monday Tuesday Wednesay it’s interpolating the array so it puts spaces between elements – this is what happens when you put an array in double quotes

So …. So – if your strings contain newlines no newlines print

Print Cont’d print is looking for a list of strings to print its arguments are evaluated in list context <> will return a list of lines in list context so: print ; # similar to the cat command print sort <>; #similar to Unix sort command

Revisit Parenthesis print (“Hello, World\n”); print “Hello, World\n”; if the invocation of print looks like a function call, then it is a function call print (5*4); #prints 20 but also contains a return value – true / false usually always succeeds unless I/O error

Return Value Cont’d print (5*4) +3; error prints 20 but return value is 1 – is 4 – logic error. If it looks like a function – it is a function applies to all Perl functions

Formatted Output with printf C’s printf is similar to Perl’s printf take a format string followed b a list of things to print the format string is a fill-in-the-blanks template with the desired form of output. printf “Hello, %s; your password expires in %d days!\n”, $user, $days_to_expire;

printf conversions begin with % should be the same number of items in the list as there are conversions don’t match – don’t work common conversions: –%s – string, %g – auto chooses int, fp or exp –%d – decimal point – value is truncated not rounded

printf Cont’d field width: –printf “%6d\n”, 42 # –printf “%2d\n”, 2e ; #2001 %s – string it interpolates the given value as a string printf “10%s\n”, “perl”; # perl negative field left justifies printf “-15%s\n”, “perl”; #perl

%f rounds off its output as needed even lets you request number of digits after the decimal pt. printf “%12f\n”, 6*7 + 2/3; # printf “%12.3f\n”, 6*7 + 2/3; # printf “%12.0f\n”, 6*7 + 2/3; # to print a real % sign use %

Arrays and printf You won’t use an array as an argument to printf = qw( soap shampoo conditioner); my $format = “The items are: \n”. (“%10s\n” printf This uses the x operator to replicate the given string a number of times given That’s 3 in this case – look at our array values output prints item on its own line – right justified in a ten character column We once in a list context and once in a scalar context

/exercises Perform 2, 3 exercises on page 97